Escolar Documentos
Profissional Documentos
Cultura Documentos
Programming
TCL Introduction
Tcl is a general purpose scripting language.
While it can do anything other languages could possibly do, its
integration with other languages has proven even more powerful.
The strength of Tcl is
Simplicity
It is not necessary to declare a data type for variable prior to the
usage.
At runtime, Tcl interprets the codes line by line and converts the
string into appropriate data type (e.g., integer).
Execution of Tcl file
If you write some code to a file- create tcl file as given bellow.
Name of the file. Extension (tcl)
"hello.tcl"
you want to execute it, you would do it like so:
tclsh hello.tcl
Directly In Console
Type "tclsh" in the console.
Type tcl code into console.
Give enter to get result.
<- Wrong!
<- OK
Everything is a command
Everything in Tcl is a command followed by a number of arguments, which
are separated by whitespace.
PUTS
Puts command that takes only two arguments.
% puts stdout Hello
Hello
stdout- output stream
The output stream argument is optional for puts and if not included it
defaults to stdout.
% expr 3+2*5
13
% expr (3+2)*5
25
BRACKETING
We want to print the result of the expr command.
% puts expr 6 * 7
wrong # args
% set x 0
0
% puts $arr($x)
30
If we wanted to get the length of the array, we can issue the following
command.
% set length [array size arr]
start from 0
The array set command requires a list of index/value pairs. This
example sets the array called days:
array set days { Sun Sunday Mon Monday Tue Tuesday \
Wed Wednesday Thu Thursday Fri Friday Sat Saturday}
CONTROL STRUCTURES
Tcl comes with a number of built in commands for control structures
If/else
1
2
3
4
5
6
set x 5
if { $x == 1 } {
puts Not true
} else {
puts True
}
The following will give you an error.
1
2
3
4
5
set x 5
if { $x == 5 }
{
puts True
}
This is because the if command takes at least two arguments: the
condition and the body.
In the above example, one of the arguments is a newline character.
You have to put the close bracket and open bracket between
arguments on the same line. (This is similar to what we saw earlier
when defining new commands with proc).
If/else if statements
1
2
3
4
5
6
7
if { $x == 1 } {
puts first
} else if { $x == 2 } {
puts second
} else {
puts third
}
While loop
The while command takes two arguments. The first is a condition it will
check on each iteration and the second is the body of the loop.
1
2
3
4
5
6
7
set value 1
set fact 2
while { $ fact <= 5 } {
set value [expr $ v a l u e $ fact ]
incr fact
}
puts $ value
For loop
The first argument to the for command is assigned once.
The second is checked after each iteration,
The third is executed after each iteration, and the last argument is the
body of the loop.
1
2
3
4
5
set sum 0
for { set i 0 } { $ i < 10 } { incr i } {
set sum [ expr $sum + $ i ]
}
puts $sum
LISTS
A list is an ordered collection of elements such as numbers, strings or
even lists themselves. Arrays are similar to lists except that they use a
string-based index.
Creating Simple Lists
(i) % set a "1 2 3"
123
(ii) % set a {1 2 3}
123
lindex command - Retrieve an element from a list
% lindex $a 2
3
4
5
FILE I/O
Tcl includes commands to read from and write to files.
First you must open a file before you can read from or write to it, and
close it when the read and write operations are done.
To open a file, use the open command; to close a file, use the close
command.
When you open a file, specify its name and the mode in which to open
it.
If you do not specify a mode, Tcl defaults to read mode.
To write to a file, specify w for write mode
set nf [open out.nam w]
set f [open out.tr w]
You can use the puts command to write into a file
Example: Write to a File
set output [open myfile.txt w]
puts $output "This text is written to the file."
close $output
You can read a file one line at a time with the gets command.