lset.n

来自「tcl是工具命令语言」· N 代码 · 共 114 行

N
114
字号
'\"'\" Copyright (c) 2001 by Kevin B. Kenny.  All rights reserved.'\"'\" See the file "license.terms" for information on usage and redistribution'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.'\" '\" RCS: @(#) $Id: lset.n,v 1.6 2003/01/23 14:18:33 dkf Exp $'\" .so man.macros.TH lset n 8.4 Tcl "Tcl Built-In Commands".BS'\" Note:  do not modify the .SH NAME line immediately below!.SH NAMElset \- Change an element in a list.SH SYNOPSIS\fBlset \fIvarName ?index...? newValue\fR.BE.SH DESCRIPTION.PPThe \fBlset\fR command accepts a parameter, \fIvarName\fR, whichit interprets as the name of a variable containing a Tcl list. It also accepts zero or more \fIindices\fR intothe list.  The indices may be presented either consecutively on thecommand line, or grouped in aTcl list and presented as a single argument.Finally, it accepts a new value for an element of \fIvarName\fR..PPIf no indices are presented, the command takes the form:.CSlset varName newValue.CEor.CSlset varName {} newValue.CEIn this case, \fInewValue\fR replaces the old value of the variable\fIvarName\fR..PPWhen presented with a single index, the \fBlset\fR commandtreats the content of the \fIvarBane\fR variable as a Tcl list.It addresses the \fIindex\fR'th element in it (0 refers to the first element of the list).When interpreting the list, \fBlset\fR observes the same rulesconcerning braces and quotes and backslashes as the Tcl commandinterpreter; however, variablesubstitution and command substitution do not occur.The command constructs a new list in which the designated element isreplaced with \fInewValue\fR.  This new list is stored in thevariable \fIvarName\fR, and is also the return value from the \fBlset\fRcommand..PPIf \fIindex\fR is negative or greater than or equal to the numberof elements in \fI$varName\fR, then an error occurs..PPIf \fIindex\fR has the value \fBend\fR, it refers to the last elementin the list, and \fBend\-\fIinteger\fR refers to the last element inthe list minus the specified integer offset..PPIf additional \fIindex\fR arguments are supplied, then each argument isused in turn to address an element within a sublist designatedby the previous indexing operation,allowing the script to alter elements in sublists.  The command,.CSlset a 1 2 newValue.CEor.CSlset a {1 2} newValue.CEreplaces element 2 of sublist 1 with \fInewValue\fR..PPThe integer appearing in each \fIindex\fR argument must be greaterthan or equal to zero.  The integer appearing in each \fIindex\fRargument must be strictly less than the length of the correspondinglist.  In other words, the \fBlset\fR command cannot change the sizeof a list.  If an index is outside the permitted range, an error is reported..SH EXAMPLESIn each of these examples, the initial value of \fIx\fR is:.CSset x [list [list a b c] [list d e f] [list g h i]]  => {a b c} {d e f} {g h i}.CEThe indicated return value also becomes the new value of \fIx\fR(except in the last case, which is an error which leaves the value of\fIx\fR unchanged.).CSlset x {j k l} => j k llset x {} {j k l} => j k llset x 0 j => j {d e f} {g h i}lset x 2 j => {a b c} {d e f} jlset x end j => {a b c} {d e f} jlset x end-1 j => {a b c} j {g h i}lset x 2 1 j => {a b c} {d e f} {g j i}lset x {2 1} j => {a b c} {d e f} {g j i}lset x {2 3} j => \fIlist index out of range\fR.CEIn the following examples, the initial value of \fIx\fR is:.CSset x [list [list [list a b] [list c d]] \e            [list [list e f] [list g h]]] => {{a b} {c d}} {{e f} {g h}}.CEThe indicated return value also becomes the new value of \fIx\fR..CSlset x 1 1 0 j => {{a b} {c d}} {{e f} {j h}}lset x {1 1 0} j => {{a b} {c d}} {{e f} {j h}}.CE.SH "SEE ALSO"list(n), lappend(n), lindex(n), linsert(n), llength(n), lsearch(n), lsort(n), lrange(n), lreplace(n).SH KEYWORDSelement, index, list, replace, set

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?