How to convert list to string in tcl

How to convert list to string in Tcl?

+6
tcl
source share
6 answers

Most likely, you want to join , but depending on what you are trying to do, it may not be necessary.

anything in TCL can be thought of as a string at any time , therefore, you can simply use your list as a string without causing a conversion

+15
source share

If you just need the content, you can put $ listvar and it will print the contents as a string.

You can flatten the list one level or insert a delimiter character using join as the jk answer above.

Example:

% set a { 1 2 3 4 { 5 6 { 7 8 9 } } 10 } 1 2 3 4 { 5 6 { 7 8 9 } } 10 % puts $a 1 2 3 4 { 5 6 { 7 8 9 } } 10 % join $a "," 1,2,3,4, 5 6 { 7 8 9 } ,10 % join $a 1 2 3 4 5 6 { 7 8 9 } 10 
+5
source share
 set list {abcdef} for {set i 0} {$i<[llength $list]} {incr i} { append string [lindex $list $i] } puts $string 
+1
source share

Smooth list using classes:

 set list { 1 2 3 4 { 5 6 { 7 8 9 } } 10 } package require struct::list struct::list flatten -full $list 
0
source share
 set a { 1 2 3 4 { 5 6 { 7 8 9 } } 10 } set rstr [regexp -all -inline {\S+} $a] puts $rstr 
-one
source share

Use the list command.

http://wiki.tcl.tk/440

Alternatively, see the split section: http://wiki.tcl.tk/1499

 split "comp.unix.misc" 

returns "comp unix misc"

-6
source share

All Articles