Useful Tcl commands set, unset, exists

The % sign is the routine Tcl shell prompt. In the example below, we set a value of 0 to variable 'a' and then check if any information is stored in the variable.

% set a 0 (assigning the value to variable)
% if {[info exists a]} { (conditional statement to check variable)
puts "exists"
}
exists (this is the Tcl response)

% unset a (command to delete value assigned to a variable)
% if {[info exists a]} {
puts "exists"
} else { (note, Tcl does not evaluate the condition until context is complete)
puts "Unexists"
}
Unexists (Tcl response)

Following example illustrates the command structure

if { $c == "Hell" }
{
puts "Oh God!"
}
else
{
puts "Peace!"
}

In this example Tcl sees 5 words :

'if' is the first : nothing to be evaluated.
'$c == "Hell"' is the second : because of the surrounding curly braces, there is no further evaluation on this word.
'puts "Oh god !"' : for the same reason, no further evaluation
'else' : nothing to do.
'puts "Peace !"' : no further evaluation.
The first word, 'if' is seen as the command and this command is executed with as parameters the 4 following words. That is later that the condition '$c == "Hell"' is evaluated, during the execution of the if command.

Points to learn:

  • Variables store string values of any size
  • All data types are converted into strings for storing
  • Context based operations
  • Variable name versus value

    Go to Tcl-page 5... Back to Tcl-page 3... Starting page