In Tcl, you can use the
lappend command to add items on to a list, but it doesn't always do exactly what you wish it to. Let's say that I've got two lists - the first containing the early courses of a meal, and the second containing the latter courses. If I append the second list to the first, the second list gets added AS A SINGLE LIST ITEM to the first list ... in the following example, adding the list "also" with two items in it (Fruit and Coffee") to the list "lunch" with two items (Soup and Salad) results in a list of just three items - Soup, Salad and {Fruit Coffee} rather than the four I would have hoped for.
If you want to add each item in the second list on to the end of the first list, you can use the
concat command instead of the lappend command. In the following example, concat produces a 4 item list - Soup, Salad, Fruit and Coffee.
set lunch "Soup Salad"
set also "Fruit Coffee"
set first $lunch
lappend first $also
set second $lunch
set rst [concat $second $also]
puts $first
puts $rst
When I runs that ... what do I get?
earth-wind-and-fire:~/jun06 grahamellis$ tclsh ccat
Soup Salad {Fruit Coffee}
Soup Salad Fruit Coffee
earth-wind-and-fire:~/jun06 grahamellis$ (written 2006-06-27 18:34:34)
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)
[463] Splitting the difference - (2005-10-13)
[144] Tcl sandwich - lists in Tcl - (2004-12-08)
Some other Articles
Running external processes in Tcl and Tcl/TkWhich way to turn?Good follow up ... my thanksConverting between Hex and Decimal in TclTcl - lappend v concatSeparation and IntegrationThe fragility of pancakes - and better structuresWeekend in IrelandOn Crosby sandsNaming Rooms