So I've got a list, a开发者_开发百科nd 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!
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.)
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"}
精彩评论