set(n)

πŸ“¦
version 9.0.3 Β· 2 versions
, in alpine 3.23, alpine 3.24, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

✨ set(n) β€” bestie edition ✨

okay so you wanna know about set, the Tcl built-in that lives in the Tcl Built-In Commands fam and is literally the moment you read AND write variables. it's giving main character energy of every single Tcl script ever. πŸ’…


🌸 SYNOPSIS 🌸

set varName ?value?

πŸ’– DESCRIPTION πŸ’–

so here's the tea, bestie. πŸ«–

set returns the value of the variable varName. that's it. that's the first job.

but WAIT β€” if you also hand it a value, then it sets the value of varName to that value, creating a whole new variable if one doesn't already exist, and then returns that value. icon behavior honestly. ✨

now let's talk about varName itself because she has layers:

  • if varName contains an open parenthesis and ends with a close parenthesis, then it refers to an array element. the characters before the first open parenthesis are the name of the array, and the characters between the parentheses are the index within the array. πŸŽ€
  • otherwise, varName refers to a scalar variable. simple. clean. no cap.

and the resolution rules, because Tcl is a whole ecosystem:

  • if varName includes namespace qualifiers (in the array name, if it refers 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. yes you gotta go read that one too, iykyk. πŸ“š
  • if a procedure IS active and varName is unqualified, then varName refers to a parameter or local variable of the procedure β€” unless varName was declared to resolve differently through one of the global, variable or upvar commands. so basically those three commands can override the vibe. noted. 🫢

🌈 EXAMPLES 🌈

storing a random number in the variable r

set r [expr {rand()}]

storing a short message in an array element

set anAry(msg) "Hello, World!"

storing a short message in an array element specified by a variable

set elemName "msg"
set anAry($elemName) "Hello, World!"

copying a value into the variable out from a variable whose name is stored in the vbl

(and fr fr, note that it is often easier to use arrays in practice instead of doing double-dereferencing β€” the manpage said it, not me πŸ™ˆ)

set in0 "small random"
set in1 "large random"
set vbl in[expr {rand() >= 0.5}]
set out [set $vbl]

πŸ¦‹ SEE ALSO πŸ¦‹

go read your siblings, bestie, they complete the family. πŸ’œ


πŸ‘‘ KEYWORDS πŸ‘‘

read, write, variable


okay that's the whole page, and lemme just say β€” this beauty is a Tcl built-in, filed under set(n), and that's the whole source/version + page-name situation. slay, go set some variables, mwah πŸ’‹ 🌸

πŸ“¦ where this page comes from