11/06/2021

Variables in TCL , Control Structure , Loops , Switch-Case, Procedure , Variable Scope & Upvar Command


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 :


The basic structure of while loop is :

while { condition holds true } {
statement
statement
.....
statement
}

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 basic structure of the for loop in TCL is:

for { initialisation counter } { condition check counter } { Increment or Decrement counter } {
statement
statement
.....
statement
}

Please mind the gap between two consecutive curly braces ,i.e. } and {, in whatever order. Otherwise you may face syntax error.  The use of after and update commands together , adds some time delay in between the two consecutive print statement for visual delight.

The blow examples are self explanatory:

Here is the up-counting example :


Here is the down-counting example :


Here is the up-counting example with increment other than 1 :




The Foreach Loop In TCL :


The basic structure of the for-each loop in TCL is:

for counter-variable { list of scalar variables of any kind } {
 do something with $counter-variable
}

The list is like the array however we can store any kind of scalar i.e number , text etc.

Per iteration the list is exhausted from the left side by popping out the left most variable into the counter-variable. 

When the list gets exhausted the iteration stops.

Please mind the gap between two consecutive curly braces ,i.e. } and {, in whatever order. Otherwise you may face syntax error.

The below examples are self explanatory. 

The use of after and update commands together , adds some time delay in between the two consecutive print statement for visual delight.

Example-1 : the list is declared directly inside the foreach loop


Example-2 : the same list is declared outside the foreach loop


Example-3 : the same list is created from Unix Command Output


Example-4 : Accessing Multiple Lists in a single foreach loop : Use like a Look Up table


Example-5 : Accessing Multiple Lists in a single foreach loop  :  uneven number of variables are popped out from the two different lists




Switch-Case Statement in TCL :


The switch case statement in TCL have the below structure:

switch $case-variable {

<case value 1>  { statement } 
<case value 2>  { statement }
<case value 3>  { statement }
<case value 4>  { statement } 
<case value 5>  { statement }
default         { default action }

}

The case-variable in the above structure is generally a scaler.

Any of the statement can be blank also.

The below example is self-explanatory :

 


Procedure in TCL :


The procedure is similar to the function in the c-language.

The procedure can be simple structure as :

proc name-of-proc {  arguments  }  {
statement
statement
}


And it can be used as :

set variable-name [ name-of-proc <arguments separated by space> ]


The output of the procedure will be stored in $variable-name. 
Then we can use the $variable-name further in our code.

The output of the of one procedure can be piped through another procedure in a cascading manner as :

set variable-name [ proc-3 [ proc-2 [ proc-1 <arguments  separated by space> ]  ]  ]

The below example is self explanatory:
 
 

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