In this article we are going to discuss variables in TCL. We will discuss this through the below example.
Scalar Variable in TCL :
First let us see how to define a variable and print it :
set money 1900;
puts "money is = $money ";
The variable "money" is a scalar variable. We can store a single value in it. To print it we simply use the puts command.
Next let us do some mathematics with the scalar variable.
set a 10;
set b [expr $a+5];
puts "==$a===$b===";
We set a with 10 and we use expr procedure in the above-mentioned way to add 5 to the value stored in variable a.
Please notice when we define a scalar variable we do not use the symbol $ in the variable name. However whenever we are accessing the value we use $.
Next , let us see how we can empty the the content of a variable:
unset a
puts "==$a===$b===";
we use unset command to empty the content of a variable.
Then when we print the two variables , a do not give any value where as b gives 15.
Now we can check that whether a variable is defined or not by the following command :
[info exists a]
This will return a 0 value if a is defined. Adding a ! will convert 0 to 1. Now we use this along with the the below if condition :
if {![info exists a]} {
set a 50
}
This piece of code will check that if a is defined then we assign value 50 to it.
To increase the value by 1 , we can use the below command:
incr a;
puts "==$a===$b===";
This will print a = 51 and b as 15.
For your easy learning practice , the blow code is provided
If-ElseIf-Else Control Structure :
In TCL the basic structure of if-else is as follows :
if { condition is true } {
statement
} else {
statement
}
Notice that there is a space between text and { or } and between } and { . This is a must otherwise you may face syntax error.
Now if there is a lot more conditions , then to incorporate that we can write elseif rather than nested if else conditions.
if { condition-1 is true } {
statement
} elseif { condition-2 is true } {
statement
} else {
statement
}
You can run the below piece of code to understand the above-mentioned control structures.
While Loop in TCL :
To break out of the loop you may use break keyword.
To increase the counter without executing the rest of the code you may use continue keyword.
The below example gives you the the consolidated use of continue/break with the help of the if-elseif-else conditions :
For Loop In TCL :
The Foreach Loop In TCL :
Switch-Case Statement in TCL :
Procedure in TCL :
proc name-of-proc { arguments } {
statement
statement
}
set variable-name [ name-of-proc <arguments separated by space> ]
Scope of Variable in TCL :
The scope of variable can be understood by running the below example & comparing the code and the output.
For explanation please watch the first video mentioned down below this page.
Upvar in TCL :
The upvar command "ties" (makes an alias) the name of a variable in the current scope to a variable in a different scope.
This is commonly used to simulate pass-by-reference to procs.
You may run the below code example and compare the code with the output to understand the usage of the upvar.
For explanation please watch the first video down below.
Video-1 : Basics of Variables in TCL , Control Structure , Loops , Switch-Case, Procedure , Variable Scope & Upvar Command
Video-2 : ForEach Loop As Look-Up-table
Video-3 : Foreach-in-collection from EDA Tool Shell ONLY
Courtesy : www.pngegg.com