TCL Short notes

Variables

In Tcl, simple variables are defined by a name and a value, with the value always stored as a string. These variables can hold various types of data, but they are treated as strings when manipulated.

Example:

set score 100

Arrays in Tcl are collections of elements, where each element is a variable with its own name and value. Arrays are often referred to as associative arrays, as they can have arbitrary string names for both the array and its elements.

Example:

set yearTotal 0 foreach month {Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec} { set yearTotal [expr $yearTotal + $earnings($month)] }

Tcl only supports one-dimensional arrays, but you can simulate multidimensional arrays 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)
Here, $matrix(1,2) will return 218.

Types of operators in TCL:

Operators:

  • Relational operators: (<, <=, >, >=, ==, !=) return either 0 (false) or 1 (true).
    Bitwise operators: (&, |, ^, <<, >>, ~) operate on integer values and manipulate bits.

Example:

expr { $a < $b ? $a : $b }

This evaluates the minimum of $a and $b using the ternary operator (?:).


Substitutions

In Tcl, substitutions can occur in two ways for expression operands:

  1. Normal Tcl parsing: Where variables and commands are substituted before the expression is evaluated.
  2. Expression evaluator: This performs a second round of substitution as it evaluates the expression.

Example:

set x 5 expr { 2 * sin($x) }
The braces {} prevent variable substitution during parsing, but the expression evaluator handles the $x variable correctly.

To prevent infinite loops, always use braces around expressions in control structures like while.

Example:

set pow 1 while {$pow < $num} { set pow [expr $pow * 2] }

String Manipulation

Tcl allows string comparison with operators such as <, >, <=, >=, ==, and !=. When both operands are strings, Tcl compares them lexicographically. If either operand can’t be treated as a number, it defaults to string comparison.

Example:

set x 0 set y 00 if {$x == $y} { puts “They are equal!” }

In this case, Tcl compares x and y as numbers, resulting in 1 (true).

For strict string comparison, use the string compare command.


Types and Conversions

Tcl automatically converts between types when necessary, such as converting an integer to a real number or vice versa.

  • Integer and Real Conversion: Conversions between integers and real numbers happen automatically.
  • Non-Numeric Strings: When performing operations with non-numeric strings, Tcl treats them as strings and converts other operands to strings if needed.

Example:

set a 3.14 
set b 2 
set result [expr $a + $b] ; # Automatic type conversion: $a is real, $b is int

To explicitly convert between types, use commands like double or int.

Example:

set realValue [double 3.5] set intValue [round 3.5]

Precision

Tcl provides precision for both integer and real types:

  • Integers (type int): 32-bit precision.
    Reals (type double): 64-bit precision.

When converting real numbers to strings, Tcl defaults to 6 significant digits. You can adjust the precision by setting the tcl_precision global variable.

Example:

set tcl_precision 17

This ensures 17 significant digits are maintained in floating-point string conversions.


List

list is an ordered collection of elements, each separated by a space. Lists can be nested, allowing for complex data structures.

Tcl provides several commands for manipulating lists:

concat: Joins multiple lists into one.
join: Combines list elements into a single string using a specified separator.
lappend: Adds one or more elements to a list.
lindex: Returns the element at a specified index.
lrange: Extracts a sublist from a list.
lsort: Sorts a list.

Example:

set myList {apple banana cherry} lappend myList “date” puts $myList ; # Outputs: apple banana cherry date


Control Flow

Tcl provides several control flow structures:

if: Conditional branching.
while: Looping until a condition is met.
for: Standard for loop.
foreach: Iterates over a list.
switch: Matches a string against patterns.
eval: Executes dynamically created Tcl code.
Example:

set count 1 while {$count <= 5} { puts “Count: $count” incr count }


Procedures

procedure in Tcl is a reusable block of code that can accept arguments and return a value.

Syntax:

proc procedure_name {arg1 arg2} { # Code body return $result }
Example:

proc add {a b} { return [expr $a + $b] } puts [add 5 10] ; # Outputs: 15
To reference global variables, use the global command.

Arguments

Tcl allows passing arguments to procedures. You can define default argument values and use variable-length arguments.

Example:

proc multiply {a {factor 2}} { return [expr $a * $factor] } puts [multiply 3] ; # Outputs: 6 (using default factor) puts [multiply 3 4] ; # Outputs: 12 (custom factor)

Call by Reference

The upvar  command allows procedures to modify variables outside their scope, either within the same procedure or across different levels of the call stack.

Example:

set myArray(apple) 5 proc printArray {} { upvar #0 myArray arr puts $arr(apple) } printArray ; # Outputs: 5

Creating New Control Structures

Tcl allows the creation of custom control structures using uplevel, which allows a script to be executed at a different stack level.

Example:

proc repeat {times body} { for {set i 0} {$i < $times} {incr i} { uplevel $body } } repeat 3 {puts “Hello, world!”}
This prints “Hello, world!” three times.

24 thoughts on “TCL Short notes”

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!
Scroll to Top