set(3tcl)
β¨ set β the manual page, but make it cute β¨
Tcl Built-In Commands π this one lives in the set(3tcl) era and honestly? she's the blueprint.
πΈ NAME πΈ
set β read and write variables π
the most used command in all of Tcl and she's just sitting there being iconic for it.
π¦ SYNOPSIS π¦
set varName ?value?
π DESCRIPTION π
okay bestie, here's the tea. π«
set returns the value of the variable varName. that's it. that's the read. no notes. π
but if value is specified, then it sets the value of varName to value β and if that variable didn't already exist, she creates a brand new one for you, no questions asked β¨ β and then returns its value. so set with two args is a write-then-read combo, lowkey a two-for-one deal.
now here's where it gets spicy πΆοΈ β if varName contains an open parenthesis and ends with a close parenthesis, then it's referring to an array element, bestie:
- the characters before the first open parenthesis = the name of the array
- the characters between the parentheses = the index within the array
otherwise? varName is just referring to a plain old scalar variable. simple. clean. a vibe. π
the namespace lore, because it matters π
if varName includes namespace qualifiers (and yes, in the array name if it's referring to an array element), OR if varName is unqualified (meaning it does not include the names of any containing namespaces) but no procedure is active, then varName refers to a namespace variable, resolved according to the rules described under NAME RESOLUTION in the namespace manual page. π go read that one, it's giving required reading.
and if a procedure is active and varName is unqualified, then varName refers to a parameter or local variable of that procedure β unless varName was declared to resolve differently through one of the global, variable or upvar commands. so if it's acting weird, check those three first. iykyk π
π EXAMPLES π
store a random number in the variable r:
set r [expr {rand()}]
store a short message in an array element:
set anAry(msg) "Hello, World!"
store a short message in an array element specified by a variable:
set elemName "msg"
set anAry($elemName) "Hello, World!"
copy a value into the variable out from a variable whose name is stored in the vbl (and bestie, note that it's often easier to use arrays in practice instead of doing this whole double-dereferencing thing π):
set in0 "small random"
set in1 "large random"
set vbl in[expr {rand() >= 0.5}]
set out [set $vbl]
π₯ SEE ALSO π₯
expr(3tcl)global(3tcl)namespace(3tcl)proc(3tcl)trace(3tcl)unset(3tcl)upvar(3tcl)variable(3tcl)
go follow them, they're all mutuals π«Ά
β KEYWORDS β
read, write, variable
and that's the whole page, bestie!! no cap, you now officially understand set. periodt. π
β¨
this cutie came to you from Tcl β manual page set(3tcl) β slay, go set some variables ππ¦π
π¦ 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) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms