| 1 |
'\" |
| 2 |
'\" Copyright (c) 2001 Kevin B. Kenny <[email protected]>. All rights reserved. |
| 3 |
'\" |
| 4 |
'\" See the file "license.terms" for information on usage and redistribution |
| 5 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 6 |
'\" |
| 7 |
.TH lset n 8.4 Tcl "Tcl Built-In Commands" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 11 |
.SH NAME |
| 12 |
lset \- Change an element in a list |
| 13 |
.SH SYNOPSIS |
| 14 |
\fBlset \fIvarName ?index ...? newValue\fR |
| 15 |
.BE |
| 16 |
.SH DESCRIPTION |
| 17 |
.PP |
| 18 |
The \fBlset\fR command accepts a parameter, \fIvarName\fR, which |
| 19 |
it interprets as the name of a variable containing a Tcl list. |
| 20 |
It also accepts zero or more \fIindices\fR into |
| 21 |
the list. The indices may be presented either consecutively on the |
| 22 |
command line, or grouped in a |
| 23 |
Tcl list and presented as a single argument. |
| 24 |
Finally, it accepts a new value for an element of \fIvarName\fR. |
| 25 |
.PP |
| 26 |
If no indices are presented, the command takes the form: |
| 27 |
.PP |
| 28 |
.CS |
| 29 |
\fBlset\fR varName newValue |
| 30 |
.CE |
| 31 |
.PP |
| 32 |
or |
| 33 |
.PP |
| 34 |
.CS |
| 35 |
\fBlset\fR varName {} newValue |
| 36 |
.CE |
| 37 |
.PP |
| 38 |
In this case, \fInewValue\fR replaces the old value of the variable |
| 39 |
\fIvarName\fR. |
| 40 |
.PP |
| 41 |
When presented with a single index, the \fBlset\fR command |
| 42 |
treats the content of the \fIvarName\fR variable as a Tcl list. |
| 43 |
It addresses the \fIindex\fR'th element in it |
| 44 |
(0 refers to the first element of the list). |
| 45 |
When interpreting the list, \fBlset\fR observes the same rules |
| 46 |
concerning braces and quotes and backslashes as the Tcl command |
| 47 |
interpreter; however, variable |
| 48 |
substitution and command substitution do not occur. |
| 49 |
The command constructs a new list in which the designated element is |
| 50 |
replaced with \fInewValue\fR. This new list is stored in the |
| 51 |
variable \fIvarName\fR, and is also the return value from the \fBlset\fR |
| 52 |
command. |
| 53 |
.PP |
| 54 |
If \fIindex\fR is negative or greater than the number |
| 55 |
of elements in \fI$varName\fR, then an error occurs. |
| 56 |
.PP |
| 57 |
If \fIindex\fR is equal to the number of elements in \fI$varName\fR, |
| 58 |
then the given element is appended to the list. |
| 59 |
.PP |
| 60 |
The interpretation of each simple \fIindex\fR value is the same as |
| 61 |
for the command \fBstring index\fR, supporting simple index |
| 62 |
arithmetic and indices relative to the end of the list. |
| 63 |
.PP |
| 64 |
If additional \fIindex\fR arguments are supplied, then each argument is |
| 65 |
used in turn to address an element within a sublist designated |
| 66 |
by the previous indexing operation, |
| 67 |
allowing the script to alter elements in sublists (or append elements |
| 68 |
to sublists). The command, |
| 69 |
.PP |
| 70 |
.CS |
| 71 |
\fBlset\fR a 1 2 newValue |
| 72 |
.CE |
| 73 |
.PP |
| 74 |
or |
| 75 |
.PP |
| 76 |
.CS |
| 77 |
\fBlset\fR a {1 2} newValue |
| 78 |
.CE |
| 79 |
.PP |
| 80 |
replaces element 2 of sublist 1 with \fInewValue\fR. |
| 81 |
.PP |
| 82 |
The integer appearing in each \fIindex\fR argument must be greater |
| 83 |
than or equal to zero. The integer appearing in each \fIindex\fR |
| 84 |
argument must be less than or equal to the length of the corresponding |
| 85 |
list. In other words, the \fBlset\fR command can change the size |
| 86 |
of a list only by appending an element (setting the one after the current |
| 87 |
end). If an index is outside the permitted range, an error is reported. |
| 88 |
.SH EXAMPLES |
| 89 |
.PP |
| 90 |
In each of these examples, the initial value of \fIx\fR is: |
| 91 |
.PP |
| 92 |
.CS |
| 93 |
set x [list [list a b c] [list d e f] [list g h i]] |
| 94 |
\fI\(-> {a b c} {d e f} {g h i}\fR |
| 95 |
.CE |
| 96 |
.PP |
| 97 |
The indicated return value also becomes the new value of \fIx\fR |
| 98 |
(except in the last case, which is an error which leaves the value of |
| 99 |
\fIx\fR unchanged.) |
| 100 |
.PP |
| 101 |
.CS |
| 102 |
\fBlset\fR x {j k l} |
| 103 |
\fI\(-> j k l\fR |
| 104 |
\fBlset\fR x {} {j k l} |
| 105 |
\fI\(-> j k l\fR |
| 106 |
\fBlset\fR x 0 j |
| 107 |
\fI\(-> j {d e f} {g h i}\fR |
| 108 |
\fBlset\fR x 2 j |
| 109 |
\fI\(-> {a b c} {d e f} j\fR |
| 110 |
\fBlset\fR x end j |
| 111 |
\fI\(-> {a b c} {d e f} j\fR |
| 112 |
\fBlset\fR x end-1 j |
| 113 |
\fI\(-> {a b c} j {g h i}\fR |
| 114 |
\fBlset\fR x 2 1 j |
| 115 |
\fI\(-> {a b c} {d e f} {g j i}\fR |
| 116 |
\fBlset\fR x {2 1} j |
| 117 |
\fI\(-> {a b c} {d e f} {g j i}\fR |
| 118 |
\fBlset\fR x {2 3} j |
| 119 |
\fI\(-> list index out of range\fR |
| 120 |
.CE |
| 121 |
.PP |
| 122 |
In the following examples, the initial value of \fIx\fR is: |
| 123 |
.PP |
| 124 |
.CS |
| 125 |
set x [list [list [list a b] [list c d]] \e |
| 126 |
[list [list e f] [list g h]]] |
| 127 |
\fI\(-> {{a b} {c d}} {{e f} {g h}}\fR |
| 128 |
.CE |
| 129 |
.PP |
| 130 |
The indicated return value also becomes the new value of \fIx\fR. |
| 131 |
.PP |
| 132 |
.CS |
| 133 |
\fBlset\fR x 1 1 0 j |
| 134 |
\fI\(-> {{a b} {c d}} {{e f} {j h}}\fR |
| 135 |
\fBlset\fR x {1 1 0} j |
| 136 |
\fI\(-> {{a b} {c d}} {{e f} {j h}}\fR |
| 137 |
.CE |
| 138 |
.SH "SEE ALSO" |
| 139 |
list(n), lappend(n), lindex(n), linsert(n), llength(n), lsearch(n), |
| 140 |
lsort(n), lrange(n), lreplace(n), |
| 141 |
string(n) |
| 142 |
.SH KEYWORDS |
| 143 |
element, index, list, replace, set |
| 144 |
'\"Local Variables: |
| 145 |
'\"mode: nroff |
| 146 |
'\"End: |