set(3tcl)

πŸ“¦
version 9.0.4 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ 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 πŸ”₯

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