Tool Command Lang Variables Expressions
Tool Command Lang Variables Expressions
Expressions
2. associative arrays
Variables may be created, read, and modified with the set command,
The first argument is the name of a variable and the second, if present, is a
in a Tcl command.
The characters following the $ are treated as a variable name, and the $ and
Example
Example :
set x 43
incr x 12
55
Both the variables original value and the increment must be integer strings,
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 :
foreach i {1 2 3} {
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.
#!/usr/bin/tclsh
for { set index 0 } { $index < [array size languages] } { incr index } {
languages(0) : Tcl
languages(1) : C Language
#!/usr/bin/tclsh
set personA(Age) 14
Age Name
languages(0) : Tcl
languages(1) : C Language
Example :
set i 1; set j 2
218
In the above example matrix is an array having 3 elements namely (1,1), (1,2),
(1,3)
string).
Example :
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.
Integers
Decimal Octal Hexadecimal
320 0423 0x5A
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
Example :
expr 2*sin($x)
19 VIT - SENSE 12-02-2015
String Manipulation
TCL expression allow simple string operations for some operators.
Example :
..
character enclosed.
The only operators allowed for string operands are >, <, <=, >=, ==, !=.
String operations are performed only for the relational operators and only if
If the operands for an operator have different types, then TCL automatically