1lset(n) Tcl Built-In Commands lset(n)
2
3
4
5______________________________________________________________________________
6
8 lset - Change an element in a list
9
11 lset varName ?index...? newValue
12_________________________________________________________________
13
15 The lset command accepts a parameter, varName, which it interprets as
16 the name of a variable containing a Tcl list. It also accepts zero or
17 more indices into the list. The indices may be presented either con‐
18 secutively on the command line, or grouped in a Tcl list and presented
19 as a single argument. Finally, it accepts a new value for an element
20 of varName.
21
22 If no indices are presented, the command takes the form:
23 lset varName newValue
24 or
25 lset varName {} newValue
26 In this case, newValue replaces the old value of the variable varName.
27
28 When presented with a single index, the lset command treats the content
29 of the varName variable as a Tcl list. It addresses the index'th ele‐
30 ment in it (0 refers to the first element of the list). When inter‐
31 preting the list, lset observes the same rules concerning braces and
32 quotes and backslashes as the Tcl command interpreter; however, vari‐
33 able substitution and command substitution do not occur. The command
34 constructs a new list in which the designated element is replaced with
35 newValue. This new list is stored in the variable varName, and is also
36 the return value from the lset command.
37
38 If index is negative or greater than or equal to the number of elements
39 in $varName, then an error occurs.
40
41 The interpretation of each simple index value is the same as for the │
42 command string index, supporting simple index arithmetic and indices │
43 relative to the end of the list.
44
45 If additional index arguments are supplied, then each argument is used
46 in turn to address an element within a sublist designated by the previ‐
47 ous indexing operation, allowing the script to alter elements in sub‐
48 lists. The command,
49 lset a 1 2 newValue
50 or
51 lset a {1 2} newValue
52 replaces element 2 of sublist 1 with newValue.
53
54 The integer appearing in each index argument must be greater than or
55 equal to zero. The integer appearing in each index argument must be
56 strictly less than the length of the corresponding list. In other
57 words, the lset command cannot change the size of a list. If an index
58 is outside the permitted range, an error is reported.
59
61 In each of these examples, the initial value of x is:
62 set x [list [list a b c] [list d e f] [list g h i]]
63 → {a b c} {d e f} {g h i}
64 The indicated return value also becomes the new value of x (except in
65 the last case, which is an error which leaves the value of x
66 unchanged.)
67 lset x {j k l}
68 → j k l
69 lset x {} {j k l}
70 → j k l
71 lset x 0 j
72 → j {d e f} {g h i}
73 lset x 2 j
74 → {a b c} {d e f} j
75 lset x end j
76 → {a b c} {d e f} j
77 lset x end-1 j
78 → {a b c} j {g h i}
79 lset x 2 1 j
80 → {a b c} {d e f} {g j i}
81 lset x {2 1} j
82 → {a b c} {d e f} {g j i}
83 lset x {2 3} j
84 → list index out of range
85 In the following examples, the initial value of x is:
86 set x [list [list [list a b] [list c d]] \
87 [list [list e f] [list g h]]]
88 → {{a b} {c d}} {{e f} {g h}}
89 The indicated return value also becomes the new value of x.
90 lset x 1 1 0 j
91 → {{a b} {c d}} {{e f} {j h}}
92 lset x {1 1 0} j
93 → {{a b} {c d}} {{e f} {j h}}
94
96 list(n), lappend(n), lindex(n), linsert(n), llength(n), lsearch(n),
97 lsort(n), lrange(n), lreplace(n), string(n) │
98
99
100
102 element, index, list, replace, set
103
104
105
106Tcl 8.4 lset(n)