Tutorial TCL
Tutorial TCL
Dongsoo S. Kim
[email protected]
What is Tcl/Tk?
language
Build on-the-fly commands and procedures
Embeddable
Platform-independent
Tk:
GUI toolkit and widgets based on Tcl
Open source, cross-platform
Why Tcl/TK?
Resources
http://www.tcl.tk
Hello World
% tcl
tcl>puts "Hello. World!"
Hello. World!
tcl>exit
%
Script File
% cat hello.tcl
puts "Hello. World!"
% tcl hello.tcl
Hello. World!
%
Script as a Program
program following #!
% ls -l
-rwxr-xr-x
1 user
% which tcl
/usr/local/bin/tcl
% cat hello.tcl
#!/usr/local/bin/tcl
puts "Hello. World!"
% hello.tcl
Hello. World!
%
group
42 Jul
3 13:12 hello.tcl
Tcl Syntax
Simple syntax
command_name arg1 arg2
Assignment (set)
set income 32000
puts "The income is $income"
Comments
;# continuing
Multiple Commands in a
Line
Mathematical Expression
Tcl>set pi 3.141592
Tcl>set r 2
Tcl>expr 2*$pi*$r
12.566368
Tcl>expr sin($pi/3)
0.866025294853
11
Sub-Command Evaluation
12
if {cond} {commands}
[ elseif {cond} {commands} ]
[ else {commands} ]
set sel 3
if {$sel
puts
} elseif
puts
} else {
puts
}
== 1} {
"Selection $sel"
{$sel == 2} {
"Selection $sel"
"Invalid selection"
13
set i 1
set sum 0
while {$i <= 10} {
set sum [expr $sum+$i]
incr i
}
14
Celcius
-18
-18
15
Basic foreach
set weekdays {Sun Mon Tue Wed Thu Fri Sat}
foreach d $weekdays {
puts $d
}
foreach Variations
set Colors {red orange yellow green blue purple}
foreach {a b c} $Colors {
puts "$c--$b--$a
}
set Foods {apple orange banana lime berry grape}
foreach f $Foods c $Colors {
puts "a $f is usually $c"
}
foreach {a b} $Foods c $Colors {
puts "$a & $b are foods. $c is a color."
}
16
Procedures
Syntax
proc name arg-list proc-body
proc mile2km dist {
return [expr $dist*1.6]
}
puts "Miles \t KM"
for {set d 1} {$d < 10} {incr d} {
set km [mile2km $d]
puts "$d\t $km"
}
17
Variable scope
Lists in Tcl
19
List Operations
20
21
23
Array operations
Associative arrays (string as index)
set color(rose) red
set color(sky) blue
set color(medal) gold
set color(leaves) green
set color(blackboard) black
puts [array exists color]
(tests if an array with the name "color" exists)
puts [array exists colour]
puts [array names color] (returns a list of the index
strings)
foreach item [array names color] {
puts "$item is $color($item)"
}
(iterating through array)
set lstColor [array get color] (convert array to list)
array set color $lstColor
(convert list to array)
24
Regular expressions
regsub
set stmt "Fan is one of Shanias fans"
regsub nocase "fan" $stmt "Kristy" newStmt
?switches? exp
string
subSpec ?
varName?
puts "$newStmt"
regsub nocase all "fan" $stmt "Kristy" newStmt
puts "$newStmt"
regexp
format
formatString
?arg
25
String operations
set statement "
Fan is a student
"
set statement [string trim $statement]
puts [string length $statement]
puts [string length statement]
puts [string index $statement 4]
puts [string index $statement end]
puts [string first "is" $statement]
(string last)
puts [string first $statement "is"]
puts [string range $statement 4 end]
puts [string replace $statement 9 end "professor"]
puts [string match "*student" $statement] (* ? [])
26
File Operations
set fRead [open source.txt r]
set fWrite [open target.txt w]
while {![eof $fRead]} {
set strLine [gets $fRead] ;#or gets $fRead strLine
regsub nocase all "fan" $strLine "kristy" strLine
puts $fWrite $strLine
}
close $fRead
close $fWrite
################
source.txt
################
Fan is a CSE student.
Fan is also one of Shanias fans.
Kristy and Fan are classmates.
27
Miscellaneous commands