12.3.5         Variables in Tcl Scripts

The set command is used to write and read variables. Tcl creates the variable with the set command.  Variables allow you to store values for use later or in other commands.  They exit only inside the script and only for as long as the script runs. These script variables exist only inside the script and are not available to WebAccess displays or tags unless you use the BINDVAR or SETVAL or GETVAL commands to pass the value of the script variable to a WebAccess TAG (or read the value of the tag into the variable). For example, the following command modifies the variable x to hold the value 73:

set x 73

You don't declare variables in Tcl: a variable is created automatically the first time it is set. Tcl variables don't have types: any variable can hold any value.

To use the value of a variable in a command, use variable substitution as in the following example:

expr $x*3

When a $ appears in a command, Tcl treats the letters and digits following it as a variable name, and substitutes the value of the variable in place of the name. In this example, the actual argument received by the expr command will be 32*3 (assuming that variable x was set as in the previous example). You can use variable substitution in any word of any command, or even multiple times within a word:

set cmd expr

set x 10

$cmd $x*$x

For example, the following set of commands modifies the variable x2 to hold the value of tag4 where tag4 is an IO Tag. 

BINDVAR x2 tag4
Set x2 10

SETVAL tag4=10

 

The set x2 11 is equivalent to SETVAL tag4=11