Version 13 of lappend

Updated 2004-10-26 15:44:11

lappend varName ?value value ...?


http://www.purl.org/tcl/home/man/tcl8.4/TclCmd/lappend.htm


Appends each value as a list element onto the end of the list stored in varName.

lappend used to be the only list-command which requires the name of a variable containing a list rather than the list itself, but in Tcl 8.4 the lset command was introduced.

 lappend listName

rather than

 lappend $listName

This command differs from append in that values are appended as list elements rather than raw text.

lappend will not make varName a list ; if it is one before the command, it will continue to be one.

AJD Uh? lappend *will* attempt to make varName's value into a list. The docs say "This command treats the variable given by varName as a list." ie. if it isn't a list to begin with, tcl will attempt to convert it to one, giving an error this is not possible:

 % set x "a \{" ;# string
 a {
 % lappend x b ;# treat it as list
 unmatched open brace in list ;# it wasn't a proper list!

You do not need to assign the results of lappend to varName; lappend modifies varName directly.


How would one append an item to the beginning of a list? Since append means to the end of, one wouldn't append to the beginning of a list. Instead, for a way to perform the equivalent of prepend, see linsert -- jfr


TV (Oct 26 '04) Just because I happened to stumble accross this:

 (Tcl) 98 % set r {}
 (Tcl) 99 % lappend r {1 2}
 {1 2}
 (Tcl) 100 % set r {1 2}
 1 2

You "stumbled across" proper list quoting? What? The first $r is a list with one element: "1 2" The second $r is a list with two elements: "1" and "2"


See also list, lindex, linsert, llength, lrange, lreplace, lsearch, lsort .


Tcl syntax help - Arts and crafts of Tcl-tk programming - Category Command