Variables and Expressions


The following two expressions can be used to assign a value to a variable (here, the variable a), either in a command input file or interactively:

set a 3.4
$a = 3.4
The second example is part of the Biosym/MSI extension of TCL. Entering these commands interactively causes BTCL to print the resulting value, 3.4.

To find the value of a you can use either:

set a
echo $a
Again, entering these expressions interactively causes the current value of a to be printed. If a is undefined, however, an error message is printed. This would happen if, for example, you entered the following at the BTCL prompt:

BTCL >   set a "hello"
BTCL >   unset a
BTCL >   set a
In general, whenever $a is encountered (where a could be any variable name), the string $a is replaced by its value. The reason echo $a is cited in the above example is because here, $a at the start of a new command line would have been interpreted as the command 3.4--which would have produced an error. This is seen more clearly in the following example script, where the commands are preceded by BTCL >, and the output is not:

BTCL >   set a "echo" 
 echo
BTCL >   $a BIOSYM 
 BIOSYM
Only the value resulting from the command processed last is printed:

BTCL >   set a 3 ; $b = 4 ; set c "last set" 
 last set
BTCL >   echo $a $b $c 
 3 4 last set
In this example, a semicolon (;) is used to separate distinct commands that are entered on the same line.

A command line is usually ended with a carriage return. However, a command can be continued onto a subsequent line:

For example:

BTCL >   set a \ 
         {Almost 
         a 
         sentence... 
         } 
 
 Almost a sentence...
The last, and most important, difference between set a and $a = is that set can take only one value argument, but $a = can take any number of arguments and evaluate them as an expression. Consider the following example script:

BTCL >   set a 3*4 
 3*4
BTCL >   $a = 3 * 4 
 12
It is important to note that the syntax $varName = something is valid only for numeric expressions. For example, $a = hello would give an error, because the expression evaluates to a string constant.


Main access page Advanced-Use access.

BTCL Language access Brief Introduction The eval and expr Commands

Copyright Biosym/MSI