user791953
user791953

Reputation: 639

How do you delimit by "," instead of space in a TCL list?

So I've got a list, and in TCL the items are stored as {"1" "2" "3"}, space seperated. I'd like to convert this list to being {"1","2","3"} -- what would be an efficient way of doing this, aside from going through a foreach and lappending , to the end of each item?

Thanks!

Upvotes: 1

Views: 390

Answers (2)

kostix
kostix

Reputation: 55443

You're confusing string representation of a list with its in-memory representation. A list in memory is just that--an ordered list of otherwise disjoint elements. If you need to "pretty print" it (for output to a terminal, typically) make a string from it, and output it then. The simplest way to make a string in your case is to use [join] as was already suggested.

In other words, don't be deceived by the fact that the code

set L [list 1 2 3]
puts $L

outputs "1 2 3": this does not mean that "a list is stored as a string with its elements space-separated". It's just the list's default string representation used by Tcl when you ask it to implicitly make a string out of a list by passing that list to [puts] which expects a string value. (NB strictly speaking, "expects a string value" is not correct with regards to the Tcl internals, but let's ignore this for now.)

Upvotes: 4

TrojanName
TrojanName

Reputation: 5355

Your question doesn't really make sense to me. In TCL, lists are stored internally as strings. When you say you want to list to {"1","2","3"}, I can only assume you are referring to the external display of the list. That can be done using the join command as follows:

% set x [list 1 2 3]

1 2 3

% set  z "\{\"[join $x "\","]\"\}"

{"1",2",3"}

Upvotes: 4

Related Questions