Skip to content
Tcl Source Code Artifact [ebb54dfe5d]
Login

Artifact [ebb54dfe5d]

Artifact ebb54dfe5dc0e57b4a1dea667e3b97ed255bb1607b3aed9d59cc4768d6d1a1e8:



CommandName: set ManualSection: n Version: unknown TclPart: Tcl TclDescription: Tcl Built-In Commands Links:

Name

set - Read and write variables

Synopsis

::: {.synopsis} ::: [set]{.cmd} [varName]{.arg} [value]{.optarg} :::

Description

Returns the value of variable fIvarNamefR. If fIvaluefR is specified, then set the value of fIvarNamefR to fIvaluefR, creating a new variable if one does not already exist, and return its value. If fIvarNamefR 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 fIvarNamefR refers to a scalar variable.

If fIvarNamefR includes namespace qualifiers (in the array name if it refers to an array element), or if fIvarNamefR is unqualified (does not include the names of any containing namespaces) but no procedure is active, fIvarNamefR refers to a namespace variable resolved according to the rules described under fBNAME RESOLUTIONfR in the fBnamespacefR manual page.

If a procedure is active and fIvarNamefR is unqualified, then fIvarNamefR refers to a parameter or local variable of the procedure, unless fIvarNamefR was declared to resolve differently through one of the fBglobalfR, fBvariablefR or fBupvarfR commands.

Examples

Store a random number in the variable fIrfR:

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 fIoutfR from a variable whose name is stored in the fIvblfR (note that it is often easier to use arrays in practice instead of doing double-dereferencing):

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