Tcl "Join" Command Not Merging List into Single String
With the "Join" Command I've Assumed Tcl Would Merge a List of Elements into a Single String with Delimiter. However This Is Not What I See at My Terminal...
With the "join" command I've assumed tcl would merge a list of elements into a single string with delimiter.
However this is not what I see at my terminal. Also, without a delimiter it returns the same list of elements with a space in between although ideally it should merge them with no spaces
Example:
## Setting original string
set A [list 1 2 3]
% 1 2 3
puts [llength $A]
% 3
## Join list without delimiter
set B [join $A]
% 1 2 3
puts [llength $B]
% 3
## Join list with space delimiter (actual requirement)
set C [join $A " "]
% 1 2 3
puts [llength $C]
% 3
## Join list with comma delimiter (to also visibly check what happens to each element of list)
set D [join $A ","]
% 1, 2, 3
puts [llength $D]
% 3
foreach item $D {puts $item}
1,
2,
3
Not sure what is going wrong here. I am trying to set a variable as a single string "1 2 3"
Trying to merge all elements of a list into single string. However "join" returns the same list as initial but with delimiter added to each element of list (except last).
EDIT: On my new machine, the [join $A ","] is working correctly as 1,2,3 without spaces.
1 Answer
The highly unlikely bit is this:
set A [list 1 2 3]
# ==> 1 2 3
set D [join $A ","]
# ==> 1, 2, 3
as when I put that into a fresh Tcl session I instead get a final output of 1,2,3 (and that's not behaviour anyone's planning to change). I'm guessing you have a stray space in there or have defined a custom version of join.