TCL Variables &
Expressions
Prof. C. Prayline Rajabai
Assistant Professor
SENSE
VIT University
TCL Variables
Tcl supports two kinds of variables:
1. simple variables and
2. associative arrays
A simple Tcl variable consists of two things: a name and a value.
Variable names usually consists of a combination of letters, digits, and
underscores, since that makes it easier to use variable substitution.
Variables may be created, read, and modified with the set command,
Set command takes either one or two arguments.
The first argument is the name of a variable and the second, if present, is a
new value for the variable
2 VIT - SENSE 12-02-2015
TCL Variable Substitution
Variable substitution is triggered by the presence of an unquoted $ character
in a Tcl command.
The characters following the $ are treated as a variable name, and the $ and
name are replaced in the word by the value of the variable.
Sample command examples given below
Sample Command Result
set b 66 66
set a b b
set a $b 66
set My name prayline prayline
set a $b+$b+$b 66+66+66
set a $b.3 66.3
set a $b4 no such variable
3 VIT - SENSE 12-02-2015
Removing variables: unset
The unset command destroys variables.
It takes any number of arguments, each of which is a variable name, and
removes all of the variables.
Future attempts to read the variables will result in errors.
Example
unset a price earnings(January)
4 VIT - SENSE 12-02-2015
Incr and Append
incr and append provide simple ways to change the value of a variable.
incr takes two arguments : the name of a variable and an integer.
If the second argument is omitted it takes the default integer as 1.
incr command adds the integer to the variables value.
Example :
set x 43
incr x 12
55
Both the variables original value and the increment must be integer strings,
either in decimal, octal (indicated by a leading 0), or hexadecimal (indicated
by a leading 0x).
5 VIT - SENSE 12-02-2015
Incr and Append
append command adds text to the end of a variable.
It takes two arguments : the name of the variable and the new text to add.
It appends the new text to the variable and returns the variables new value.
Example :
set msg ""
foreach i {1 2 3} {
append msg "$i squared is [expr $i*$i]\n"
set msg
1 squared is 1
2 squared is 4
3 squared is 9
6 12-02-2015
VIT - SENSE
Variable manipulation commands
Command Meaning
append varName value ?value ...? Appends each of the value arguments to variable varName, in
order. If varName doesnt exist then it is created with an empty
value before appending. The return value is the new value of
varName.
incr varName ?increment? Adds increment to the value of variable varName. Increment and
the old value of varName must both be integer strings (decimal,
hexadecimal, or octal). If increment is omitted then it defaults to 1.
The new value is stored in varName as a decimal string and
returned as the result of the command.
set varName ?value? If value is specified, sets the value of variable varName to value. In
any case the command returns the (new) value of the variable.
unset varName ?varName Deletes the variables given by the varName arguments. Returns an
varName ...? empty string.
7 VIT - SENSE 12-02-2015
Arrays
An array is a collection of elements, each of which is a variable with its own
name and value.
The name of an array element has two parts:
Name of the array and
Name of the element within that array.
Tcl arrays are sometimes called associative arrays .
Array elements are referenced using notation like
array_name (element_name).
Arrays are declared or initialized using the set command.
8 VIT - SENSE 12-02-2015
Example for Arrays
set name(first) "Mary"
set name(last) "Poppins"
puts "Full name: $name(first) $name(last)"
9 VIT - SENSE 12-02-2015
Array size
The syntax for calculating the size of an array is shown below.
[array size variablename]
An example program to print the array size
#!/usr/bin/tclsh
set languages(0) Tcl
set languages(1) {C Language}
puts [array size languages]
The above code when executed produces the output as 2.
10 VIT - SENSE 12-02-2015
Array iteration example
#!/usr/bin/tclsh
set languages(0) Tcl
set languages(1) C Language"
for { set index 0 } { $index < [array size languages] } { incr index } {
puts "languages($index) : $languages($index)"
When above code is executed, it produces following result.
languages(0) : Tcl
languages(1) : C Language
11 VIT - SENSE 12-02-2015
Array indices
The syntax for retrieving indices of array is shown below.
[array names variablename]
An example to print the array indices is shown below.
#!/usr/bin/tclsh
set personA(Name) "Dave"
set personA(Age) 14
puts [array names personA]
When above code is executed, it produces following result.
Age Name
12 VIT - SENSE 12-02-2015
Array iteration example
#!/usr/bin/tclsh
set languages(0) Tcl
set languages(1) C Language"
foreach index [array names languages] {
puts "languages($index) : $languages($index)"
When above code is executed, it produces following result.
languages(0) : Tcl
languages(1) : C Language
13 VIT - SENSE 12-02-2015
TCL Multidimensional Arrays
Tcl only implements one-dimensional arrays.
Multi-dimensional arrays can be simulated by concatenating multiple indices
into a single element name.
Example :
set matrix(1,1) 140; set matrix(1,2) 218; set matrix(1,3) 84
set i 1; set j 2
set cell $matrix($i,$j)
218
In the above example matrix is an array having 3 elements namely (1,1), (1,2),
(1,3)
14 VIT - SENSE 12-02-2015
Reading user input
Value of a variable can be read from the standard input using gets (get
string).
Example :
puts "Please tell me your name."
gets stdin Name
puts "Hello, $Name!"
15 12-02-2015
VIT - SENSE
TCL Expressions
TCL Expression
expr is used for representing mathematical expression.
It combines the values of the operands with the operators to produce new
values.
Expression operands are normally integers or real numbers.
Integers are usually specified in decimal.
Classified based on the first and the second character
Integers
Decimal Octal Hexadecimal
320 0423 0x5A
17 VIT - SENSE 12-02-2015
Operators and Precedence
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations.
Arithmetic
+, -, *, /, %
Relational
==, !=, >, <, >=, <=
Logical
&&, ||, !,
Bitwise
&, |, ^, <<, >>
Ternary
?:
18 VIT - SENSE 12-02-2015
Math Functions
TCL expression support a number of mathematical functions and few are listed
below.
Function Description
abs arg Calculates the absolute value of arg.
cos arg Calculates the cosine of arg.
cosh arg Calculates the hyperbolic cosine of arg.
exp arg Calculates an exponential function (e to the power of arg).
log arg Calculates the natural logarithm of arg.
pow x y Calculates the value of x raised to the power y.
Example :
expr 2*sin($x)
19 VIT - SENSE 12-02-2015
String Manipulation
TCL expression allow simple string operations for some operators.
Example :
if {$x == New York} {
..
To specify a string operand, you must either enclose it in quotes or braces or
use variable or command substitution.
If a string is enclosed in quotes then the TCL performs the substitution of
character enclosed.
If a string is enclosed in braces, then no substitution is performed.
The only operators allowed for string operands are >, <, <=, >=, ==, !=.
20 VIT - SENSE 12-02-2015
Types and Conversions
TCL evaluates expressions numerically whenever possible.
String operations are performed only for the relational operators and only if
one or both the operands is not a number.
If the operands for an operator have different types, then TCL automatically
converts one of them to the other type.
21 VIT - SENSE 12-02-2015
Thank You