Mallikarjunarao Kosuri
Mallikarjunarao Kosuri

Reputation: 1111

how to convert a list into string in tcl

How do I convert a list into string in Tcl?

Upvotes: 9

Views: 37941

Answers (7)

Omer Kasher
Omer Kasher

Reputation: 1

Here:

% join {a b c} " and "

a and b and c

% join {a b c} ""

abc

(source: https://wiki.tcl-lang.org/page/join)

Upvotes: 0

Prince Bhanwra
Prince Bhanwra

Reputation: 192

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

Upvotes: 1

user2962675
user2962675

Reputation: 1

set a { 1 2 3 4 { 5 6 { 7 8 9 } } 10 }
set rstr [regexp -all -inline {\S+} $a]
puts $rstr

Upvotes: -1

Jashmikant Mohanty
Jashmikant Mohanty

Reputation: 11

set list {a b c d e f}
for {set i 0} {$i<[llength $list]} {incr i} {
    append string [lindex $list $i]
}
puts $string

Upvotes: 1

Michael Mathews
Michael Mathews

Reputation: 382

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

Upvotes: 5

jk.
jk.

Reputation: 14004

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

Upvotes: 20

Martin Eve
Martin Eve

Reputation: 2751

Use the list command.

http://wiki.tcl.tk/440

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

split "comp.unix.misc"

returns "comp unix misc"

Upvotes: -7

Related Questions