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 zero or more elements
16 of list with the element arguments. first and last are index values
17 specifying the first and last elements of the range to replace. The
18 index values first and last are interpreted the same as index values
19 for the command string index, supporting simple index arithmetic and
20 indices relative to the end of the list. 0 refers to the first element
21 of the list, and end refers to the last element of the list.
22
23 If either first or last is less than zero, it is considered to refer to
24 before the first element of the list. This allows lreplace to prepend
25 elements to list. If either first or last indicates a position greater │
26 than the index of the last element of the list, it is treated as if it │
27 is an index one greater than the last element. This allows lreplace to │
28 append elements to list.
29
30 If last is less than first, then any specified elements will be
31 inserted into the list before the element specified by first with no
32 elements being deleted.
33
34 The element arguments specify zero or more new elements to be added to
35 the list in place of those that were deleted. Each element argument
36 will become a separate element of the list. If no element arguments
37 are specified, then the elements between first and last are simply
38 deleted.
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
66 Appending elements to the list; note that end+2 will initially be │
67 treated as if it is 6 here, but both that and 12345 are greater than │
68 the index of the final item so they behave identically: │
69
70 % set var {a b c d e} │
71 a b c d e │
72 % set var [lreplace $var 12345 end+2 f g h i] │
73 a b c d e f g h i │
74
76 list(n), lappend(n), lindex(n), linsert(n), llength(n), lsearch(n),
77 lset(n), lrange(n), lsort(n), string(n)
78
80 element, list, replace
81
82
83
84Tcl 7.4 lreplace(n)