In Tcl,
a list is simply a string that's held in the same format (i.e. with the same delimiters and protection) as a series of parameters to a Tcl command. The split command (which does a great deal in languages such as Perl and PHP) really does little more than change the delimiters if need be, and add protective \ { and } characters - again only if need be.
Here's an example where we're splitting and manipulating the contents of a sandwich.
tcl>
set sarnie "cheese, marmite, onion, pickle"
tcl>
set d4 [split $sarnie ,]
tcl>
set d4
cheese { marmite} { onion} { pickle}
At this point, the braces have been added to protect the space character which is part of the individual ingredient
tcl>
foreach ingrid $d4 {
=>
puts [string trim $ingrid]
=>
}
cheese
marmite
onion
pickle
I used string trim to remove the space before printing each element out
Let's rejoin the ingredients into a string using the word "and" as the glue
tcl>
set d5 [join $d4 " and "]
tcl>
set d5
cheese and marmite and onion and pickle
or as an alternative, print it out sorted into the order of ingedients with my favourite first ;-)
tcl>
lsort $d4
{ marmite} { onion} { pickle} cheese
tcl>
(written 2004-12-08 01:50:44)
Associated topics are indexed under
T206 - Tcl/Tk - Lists [2472] split and join in tcl and expect - (2009-10-23)
[2468] What are Tcl lists? - (2009-10-22)
[1601] Replacing the last comma with an and - (2008-04-04)
[1405] Sorting in Tcl - lists and arrays - (2007-10-24)
[1402] Tcl - append v lappend v concat - (2007-10-23)
[1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
[1283] Generating traffic for network testing - (2007-07-29)
[1282] Stringing together Tcl scripts - (2007-07-29)
[781] Tcl - lappend v concat - (2006-06-27)
[463] Splitting the difference - (2005-10-13)
Some other Articles
Programming in isolationRecent technical articlesexample of Tcl namespaces and packagesa comment on commentsTcl sandwich - lists in TclNetwork CameraColour for accessToo technical?Comparison Chart for Perl programmers - list functionsJust provide a room and the students