Introduction to TCL

 

1) Presentation

This introduction is provided for those who do not know TCL and want to change the way Proastro is interpreting horoscope or calculate the Ayanamsa.

TCL is a script language created by John K. Ousterhout, a Berkeley doctor in computer science.

TCL has 2 main strong positive aspects:

For the programmer: TCL provides a way to start, in a C or C++ program, a TCL interpreter that runs a script file with instructions modifiable by the final user of the application.

It is also possible to add custom commands to the interpreter, providing an interface between internal data of the application and the TCL script.

It is also possible to get data from the TCL script to the program, for example compute a value in the script file and give the result to the application.

For the final user, knowing that parts of the application are TCL script provide an easy way to tailor the application behavior to meet his own needs.

2) Writing a TCL script

Remark:

To become familiar with TCL, run tclsh and try the examples below.

a)Variables:

1) #

Comment start with #

Example:

# this is a comment

set x 0 ; # comment at end of line

There are two instructions above:

set x 0

and

# comment at end of line

for {set i 0} \
{$i < 10} \
{incr i} \
{put $i}

This is an example of \ to write instruction longest that one line.

2) set

Affectation of variables is done with the set instruction.

Set is also used to declare variables.

# Example 
set a 12.6 
# declares a variable called a and gives it value 12.6

To get the value of a variable, use the $ sign

In the above example, the a value is 12.6 and is retrieved by $a.

# Example
# declares variable named a and gives it the value 44
set a 44
# declares variable named b and gives it the value of a
set b $a 
# here a and b both value is 44

3) expr

Use expr to get the value of an expression.

# example
set x 4
set y [expr $x + 10]
# x value is 4 and y value is 14

Square brackets tell the TCL interpreter that $x + 10 is an independent TCL script. Result of expr $x + 10 is 14 so y get the value 14.

4) incr

Add a value to a variable

# example 
set x 10
incr x
# here x value is 11, because incr without argument is the same as incr 1
incr x 2

# here x value is 13 because we have added 2 to 11

5) eval

Eval the expression between { } as a TCL script.

# example
eval {set x 2}
# x value is then 2

b) control instructions:

1) if

#example 

if {$x > 0} {
	set b 1
}

Here, if x is greater than zero, and is not zero, b gets the value 1.

Here is an example with else:

if { $c > 2} {
set d 4
} else { 
set e 15 
}

Important Remark:

The open brace must not be positioned at the beginning of a new line, but must be on the same line that the word it applies to. For example:

if {$x>0}
{
 set y 2
}

is not correct because the TCL interpreter expects two argument at least after if and it finds only one, {$x>0}.

The above example must be written:

if {$x>0} {
set y 2

}

or

if {$x>0} \
{ \
 set y 2 \
}

2) while

set i 10
set y 0
while {$i > 0} {
 set y [ expr $y + $i]
 incr i -1
}

At the end of the loop, y value is 55 (55 = 10 + 9 + 8 + 7 +..+ 1)

3) for

set y 0
for {set i 0} {$i<15} {incr i} {
  incr y
 }

At the end of the loop, y value is 15.

As in C language,

- The first instruction is the initialization of the for loop, performed only once.

- The second instruction is the stop condition, checked at each loop.

- The third instruction is done after each loop.

- The forth instruction is what to do for each loop.

4) foreach

foreach {i} { 0 1 2 3 } {
set myarray($i) 5
}

Here foreach execute the instruction

set myarray($i) 5

4 times

with i from 0 to 3

Set myarray(0) create, if necessary, an array called myarray and add to it an item called 0 which value is 5.

To get, for example, the value of the second item in the array, type $myarray(1).

remark: you can use break and continue, as in C to exit or continue in a loop.

set a {one two three four five}
foreach i $a {
 put $i
if {$i == "four"} break
}

output

one

two

three

four

Give attention to the way foreach is written, pointing out that the second argument of foreach is a list.

5) switch

set a 0
set b 0
set c 0

set d 1 

switch $d {
  0 {incr a}
  1 {incr b}
  2 {incr c}
}

After this short program, a is 0, b is 1, c is 0.

Unlike C language, you do not need to put a break statement after each case of the switch.

c) procedures

A TCL procedure is the same as a C function.

proc  multipli {a b} {

if {$a==0} {return 0}
if {$b==0} {return 0}
expr $a*$b

# or
# return [ expr $a*$b ]

}

Here the name of the procedure is multipli, taking two arguments, a and b

to uses multipli, type:

set x [ multipli 15 10 ]

.

.

x is then 150

If a or b is zero, we return zero. Else, the return value is the result of the last expression in the procedure, here expr $a*$b

We can also make an explicit return with return [ expr $a*$b ]

Remark :

- variables declared inside a procedure are local to the procedure.

- to reach global variables inside a procedure, use global.

proc incry x {
 global y 
 incr y $x
}

set y 10
incry 100
put $y

output:110

y value is then 110

See also:

For more information, have a look at http://www.sunlabs.com/research/tcl web site.

Bibliography