set(n)
β¨ 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
varNamecontains 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,
varNamerefers to a scalar variable. simple. clean. no cap.
and the resolution rules, because Tcl is a whole ecosystem:
- if
varNameincludes namespace qualifiers (in the array name, if it refers to an array element), OR ifvarNameis unqualified (meaning it does not include the names of any containing namespaces) but no procedure is active β thenvarNamerefers to a namespace variable, resolved according to the rules described under NAME RESOLUTION in thenamespacemanual page. yes you gotta go read that one too, iykyk. π - if a procedure IS active and
varNameis unqualified, thenvarNamerefers to a parameter or local variable of the procedure β unlessvarNamewas declared to resolve differently through one of theglobal,variableorupvarcommands. 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
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- 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