Tcl - lappend v concat
Archive - Originally posted on "The Horse's Mouth" - 2006-06-27 18:34:34 - Graham EllisIn 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$