lset(3tcl)
π lset β the glow-up command for your lists π
ok so we're talking about lset, a built-in Tcl command, bestie. it's the one that lets you reach into a list, poke one little element, and swap it out for something new. no cap.
β¨ NAME β¨
lset β change an element in a list π
πΈ SYNOPSIS πΈ
lset varName ?index ...? newValue
π¦ DESCRIPTION π¦
okok so here's the tea. lset takes a parameter, varName, and it's like "yes queen, this is the name of a variable holding a Tcl list." π
then it also accepts zero or more indices pointing into that list. those indices can show up two ways:
- lined up one after another right there on the command line, or
- all grouped together in a Tcl list and handed over as one single argument.
and finally it takes a newValue for one element of varName. periodt.
no indices? no problem π«Ά
if you present no indices at all, the command looks like this:
lset varName newValue
or
lset varName {} newValue
in that case, newValue straight-up replaces the old value of the variable varName. like, total main character takeover.
one index, one glow-up β
when you give it a single index, lset treats the content of the varName variable as a Tcl list. it addresses the index'th element in it β and reminder bestie, 0 refers to the first element of the list. π₯
when it's interpreting that list, lset observes the same rules concerning braces, quotes and backslashes as the Tcl command interpreter does. however β and this is important β variable substitution and command substitution do not occur. nope, not happening.
the command constructs a new list where the designated element is replaced with newValue. that new list gets stored in the variable varName, and it's also the return value from the lset command. double duty, iconic. π
the index rules (read this part fr fr) π
- if index is negative or greater than the number of elements in
$varName, then an error occurs. π - if index is equal to the number of elements in
$varName, then the given element is appended to the list. slay. - the interpretation of each simple index value is the same as for the command
string index, supporting simple index arithmetic and indices relative to the end of the list. iykyk.
multiple indices = going deeper π§
if additional index arguments are supplied, then each argument is used in turn to address an element within a sublist designated by the previous indexing operation. that lets your script alter elements in sublists β or append elements to sublists. nesting hours. β¨
the command:
lset a 1 2 newValue
or
lset a {1 2} newValue
replaces element 2 of sublist 1 with newValue. see how both forms do the exact same thing? cute.
the boundaries, bestie π§
- the integer appearing in each index argument must be greater than or equal to zero.
- the integer appearing in each index argument must be less than or equal to the length of the corresponding list.
in other words, the lset command can only change the size of a list by appending an element (setting the one after the current end). if an index is outside the permitted range, an error is reported. π
π EXAMPLES π
in each of these examples, the initial value of x is:
set x [list [list a b c] [list d e f] [list g h i]]
β {a b c} {d e f} {g h i}
the indicated return value also becomes the new value of x β except in the last case, which is an error which leaves the value of x unchanged. π
lset x {j k l}
β j k l
lset x {} {j k l}
β j k l
lset x 0 j
β j {d e f} {g h i}
lset x 2 j
β {a b c} {d e f} j
lset x end j
β {a b c} {d e f} j
lset 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
β {a b c} {d e f} {g h i j}
lset x {2 4} j
β list index out of range
in the following examples, the initial value of x is:
set x [list [list [list a b] [list c d]] \
[list [list e f] [list g h]]]
β {{a b} {c d}} {{e f} {g h}}
the indicated return value also becomes the new value of x. π
lset 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}}
π SEE ALSO π
list(3tcl)lappend(3tcl)lassign(3tcl)ledit(3tcl)lindex(3tcl)linsert(3tcl)llength(3tcl)lmap(3tcl)lpop(3tcl)lrange(3tcl)lremove(3tcl)lrepeat(3tcl)lreplace(3tcl)lreverse(3tcl)lsearch(3tcl)lseq(3tcl)lsort(3tcl)string(3tcl)
π« KEYWORDS π«
element, index, list, replace, set
ok that's the whole page, bestie π«Ά this one's from the Tcl 8.4 era, so go forth and glow up your lists responsibly. mwah π π
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2001 Kevin B. Kenny <kennykb@acm.org>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms