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 at the point specified by first with no elements
31 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 % lreplace {a b c d e} 1 1 foo
43 a foo c d e
44
45 Replacing two elements of a list with three:
46 % lreplace {a b c d e} 1 2 three more elements
47 a three more elements d e
48
49 Deleting the last element from a list in a variable:
50 % set var {a b c d e}
51 a b c d e
52 % set var [lreplace $var end end]
53 a b c d
54
55 A procedure to delete a given element from a list:
56 proc lremove {listVariable value} {
57 upvar 1 $listVariable var
58 set idx [lsearch -exact $var $value]
59 set var [lreplace $var $idx $idx]
60 }
61
63 list(n), lappend(n), lindex(n), linsert(n), llength(n), lsearch(n),
64 lset(n), lrange(n), lsort(n), string(n) │
65
67 element, list, replace
68
69
70
71Tcl 7.4 lreplace(n)