. Adding & Deleting members of a list .
[ Previous | Index | Next ]
The commands for adding and deleting list members are:
- concat ?arg1 arg2 ... argn?
- Concatenates the args into a single list. It also eliminates leading and trailing spaces in the arg's and adds a single separator space between arg's. Args to concat may be either individual elements, or lists. If an arg is already a list, the contents of that list is concatenated with the other args.
- lappend listName ?arg1 arg2 ... argn?
- Appends the args to the list listName treating each arg as a list element.
- linsert listName index arg1 ?arg2 ... argn?
- Returns a new list with the new listelements inserted just before the indexth element of listName. Each element argument will become a separate element of the new list. If index is less than or equal to zero, then the new elements are inserted at the beginning of the list. If index has the value end, or if it is greater than or equal to the number of elements in the list, then the new elements are appended to the list.
- lreplace listName first last ?arg1 ... argn?
- Returns a new list with N elements of listName replaced by the args. If first is less than or equal to 0, lreplace starts replacing from the first element of the list. If first is greater than the end of the list, or the word end, then lreplace behaves like lappend. If there are fewer args than the number of positions between first and last, then the positions for which there are no args are deleted.
Take a look at the example code, and pay special attention to the way that sets of characters are grouped into single list elements.
--
. Example .
set b [list a b {c d e} {f {g h}}]
puts "Treated as a list: $b\n"
set b [split "a b {c d e} {f {g h}}"]
puts "Transformed by split: $b\n"
set a [concat a b {c d e} {f {g h}}]
puts "Concated: $a\n"
lappend a {ij K lm} ;# Note: {ij K lm} is a single element
puts "After lappending: $a\n"
set b [linsert $a 3 "1 2 3"] ;# "1 2 3" is a single element
puts "After linsert at position 3: $b\n"
set b [lreplace $b 3 5 "AA" "BB"]
puts "After lreplacing 3 positions with 2 values at position 3: $b\n"
|