Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/lset.n @ 25

Last change on this file since 25 was 25, checked in by landauf, 17 years ago

added tcl to libs

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