1lreplace(n) Tcl Built-In Commands lreplace(n)
2
3
4
5______________________________________________________________________________
6
8 lreplace - Replace elements in a list with new elements
9
11 lreplace list first last ?element element ...?
12______________________________________________________________________________
13
15 lreplace returns a new list formed by replacing one or more elements of
16 list with the element arguments. first and last are index values spec‐
17 ifying the first and last elements of the range to replace. The index
18 values first and last are interpreted the same as index values for the
19 command string index, supporting simple index arithmetic and indices
20 relative to the end of the list. 0 refers to the first element of the
21 list, and end refers to the last element of the list. If list is
22 empty, then first and last are ignored.
23
24 If first is less than zero, it is considered to refer to before the
25 first element of the list. For non-empty lists, the element indicated
26 by first must exist or first must indicate before the start of the
27 list.
28
29 If last is less than first, then any specified elements will be
30 inserted into the list before the point specified by first with no ele‐
31 ments being deleted.
32
33 The element arguments specify zero or more new arguments to be added to
34 the list in place of those that were deleted. Each element argument
35 will become a separate element of the list. If no element arguments
36 are specified, then the elements between first and last are simply
37 deleted. If list is empty, any element arguments are added to the end
38 of the list.
39
41 Replacing an element of a list with another:
42
43 % lreplace {a b c d e} 1 1 foo
44 a foo c d e
45
46 Replacing two elements of a list with three:
47
48 % lreplace {a b c d e} 1 2 three more elements
49 a three more elements d e
50
51 Deleting the last element from a list in a variable:
52
53 % set var {a b c d e}
54 a b c d e
55 % set var [lreplace $var end end]
56 a b c d
57
58 A procedure to delete a given element from a list:
59
60 proc lremove {listVariable value} {
61 upvar 1 $listVariable var
62 set idx [lsearch -exact $var $value]
63 set var [lreplace $var $idx $idx]
64 }
65
67 list(n), lappend(n), lindex(n), linsert(n), llength(n), lsearch(n),
68 lset(n), lrange(n), lsort(n), string(n)
69
71 element, list, replace
72
73
74
75Tcl 7.4 lreplace(n)