[25] | 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 |
---|
| 14 | lset \- Change an element in a list |
---|
| 15 | .SH SYNOPSIS |
---|
| 16 | \fBlset \fIvarName ?index...? newValue\fR |
---|
| 17 | .BE |
---|
| 18 | .SH DESCRIPTION |
---|
| 19 | .PP |
---|
| 20 | The \fBlset\fR command accepts a parameter, \fIvarName\fR, which |
---|
| 21 | it interprets as the name of a variable containing a Tcl list. |
---|
| 22 | It also accepts zero or more \fIindices\fR into |
---|
| 23 | the list. The indices may be presented either consecutively on the |
---|
| 24 | command line, or grouped in a |
---|
| 25 | Tcl list and presented as a single argument. |
---|
| 26 | Finally, it accepts a new value for an element of \fIvarName\fR. |
---|
| 27 | .PP |
---|
| 28 | If no indices are presented, the command takes the form: |
---|
| 29 | .CS |
---|
| 30 | lset varName newValue |
---|
| 31 | .CE |
---|
| 32 | or |
---|
| 33 | .CS |
---|
| 34 | lset varName {} newValue |
---|
| 35 | .CE |
---|
| 36 | In this case, \fInewValue\fR replaces the old value of the variable |
---|
| 37 | \fIvarName\fR. |
---|
| 38 | .PP |
---|
| 39 | When presented with a single index, the \fBlset\fR command |
---|
| 40 | treats the content of the \fIvarName\fR variable as a Tcl list. |
---|
| 41 | It addresses the \fIindex\fR'th element in it |
---|
| 42 | (0 refers to the first element of the list). |
---|
| 43 | When interpreting the list, \fBlset\fR observes the same rules |
---|
| 44 | concerning braces and quotes and backslashes as the Tcl command |
---|
| 45 | interpreter; however, variable |
---|
| 46 | substitution and command substitution do not occur. |
---|
| 47 | The command constructs a new list in which the designated element is |
---|
| 48 | replaced with \fInewValue\fR. This new list is stored in the |
---|
| 49 | variable \fIvarName\fR, and is also the return value from the \fBlset\fR |
---|
| 50 | command. |
---|
| 51 | .PP |
---|
| 52 | If \fIindex\fR is negative or greater than or equal to the number |
---|
| 53 | of elements in \fI$varName\fR, then an error occurs. |
---|
| 54 | .PP |
---|
| 55 | .VS 8.5 |
---|
| 56 | The interpretation of each simple \fIindex\fR value is the same as |
---|
| 57 | for the command \fBstring index\fR, supporting simple index |
---|
| 58 | arithmetic and indices relative to the end of the list. |
---|
| 59 | .VE 8.5 |
---|
| 60 | .PP |
---|
| 61 | If additional \fIindex\fR arguments are supplied, then each argument is |
---|
| 62 | used in turn to address an element within a sublist designated |
---|
| 63 | by the previous indexing operation, |
---|
| 64 | allowing the script to alter elements in sublists. The command, |
---|
| 65 | .CS |
---|
| 66 | lset a 1 2 newValue |
---|
| 67 | .CE |
---|
| 68 | or |
---|
| 69 | .CS |
---|
| 70 | lset a {1 2} newValue |
---|
| 71 | .CE |
---|
| 72 | replaces element 2 of sublist 1 with \fInewValue\fR. |
---|
| 73 | .PP |
---|
| 74 | The integer appearing in each \fIindex\fR argument must be greater |
---|
| 75 | than or equal to zero. The integer appearing in each \fIindex\fR |
---|
| 76 | argument must be strictly less than the length of the corresponding |
---|
| 77 | list. In other words, the \fBlset\fR command cannot change the size |
---|
| 78 | of a list. If an index is outside the permitted range, an error is reported. |
---|
| 79 | .SH EXAMPLES |
---|
| 80 | In each of these examples, the initial value of \fIx\fR is: |
---|
| 81 | .CS |
---|
| 82 | set 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 |
---|
| 85 | The 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 |
---|
| 108 | In the following examples, the initial value of \fIx\fR is: |
---|
| 109 | .CS |
---|
| 110 | set 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 |
---|
| 114 | The 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" |
---|
| 122 | list(n), lappend(n), lindex(n), linsert(n), llength(n), lsearch(n), |
---|
| 123 | lsort(n), lrange(n), lreplace(n), |
---|
| 124 | .VS 8.5 |
---|
| 125 | string(n) |
---|
| 126 | .VE |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | .SH KEYWORDS |
---|
| 130 | element, index, list, replace, set |
---|