Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert a list into string in tcl

Tags:

tcl

How do I convert a list into string in Tcl?

like image 403
Mallikarjunarao Kosuri Avatar asked Jun 14 '10 09:06

Mallikarjunarao Kosuri


4 Answers

most likely what you want is join however depending on what you are trying to do this may not be necessary.

anything in TCL is able to be treated as a string at anytime, consequently you may be able to just use your list as a string without explict conversion

like image 89
jk. Avatar answered Oct 17 '22 15:10

jk.


If you just want the contents, you can puts $listvar and it will write out the contents as a string.

You can flatten the list by one level or insert a separator character by using join, as jk answered 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
like image 28
Michael Mathews Avatar answered Oct 17 '22 16:10

Michael Mathews


set list {a b c d e f}
for {set i 0} {$i<[llength $list]} {incr i} {
    append string [lindex $list $i]
}
puts $string
like image 1
Jashmikant Mohanty Avatar answered Oct 17 '22 17:10

Jashmikant Mohanty


To flatten a list using classes:

set list { 1 2 3 4 { 5 6 { 7 8 9 } } 10 }

package require struct::list
struct::list flatten -full $list
like image 1
Prince Bhanwra Avatar answered Oct 17 '22 17:10

Prince Bhanwra