简体   繁体   中英

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. 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).

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM