TCL
TCL
TCL
Documentation
books can be bought (bookstore, etc)
books in the PC lab
What is Tcl/Tk?
Tcl
a scripting language
can be extended in C (but this is harder)
ugly but simple
Tk
a simple but powerful widget set
Hello World: a complete program that exits when a person
presses the button
Tcl script =
Sequence of commands.
Commands separated by newlines, semi-colons.
Tcl command =
One or more words separated by white space.
First word is command name, others are arguments.
Returns string result.
Examples:
set
myName Saul
Arguments
x is "4 "
y is "x+10
z is "4+10
Variable Substitution
Syntax: $varName
Result
setb66
setab
seta$b
seta$b+$b+$b
seta$b.3
seta$b4
66
b
66
66+66+66
66.3
no such variable
Command Substitution
Syntax: [script]
Result
setb8
seta[expr$b+2]
seta"b3is[expr$b3]"
8
10
b3is5
Comments
Tcl Expressions
Tcl Arrays
;# 44
;# 50
;# fred 2
1,1 1,2
Lists
List-related commands:
concat
lindex
foreach
linsert
lappend
list
llength
lrange
lreplace
lsearch
lsort
Examples:
lindex {a b {c d e} f} 2
c d e
lsort {red green blue} blue green red
String Manipulation
split
string
string subcommands
compare first
match
range
trimleft
last
toupper
index
length
tolower
trim
trimright
;# this
;# Hello
;# c
Control Structures
C-like in appearance.
Commands:
if
foreach
for
while
switch
eval
break
continue
if else
set x 2
if {$x < 3} {
puts "x is less than 3"
} else {
puts "x is 3 or more"
}
while
#list
set a
set b
set i
while
reversal
{a b c d e}
"
[expr [llength $a] - 1]
{$i >= 0} {
lappend b [lindex $a $i]
incr i -1
}
puts $b
switch
set pete_count
set bob_count
set other_count 0
foreach name {Peter Peteee Bobus Me Bobor Bob} {
switch -regexp $name {
^Pete* {incr pete_count}
^Bob|^Robert {incr bob_count}
default {incr other_count}
}
}
puts "$pete_count $bob_count $other_count"
Procedures
Procedures