Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
OVERVIEW
Outline
Intro/Environment
Basic Syntax
Data Types
Flow Control
Procedure
TCL is Interpreted
(process at run-time and no need to compile)
TCL is Interactive
2. Command substitution:
The Tcl interpreter takes everything between the brackets and evaluates it as a
command
Syntax Description
list arg1 arg2 ... Creates a list out of all its arguments.
lindex list i Returns the ith element from list.
llength list Returns the number of elements in list.
lrange list i j Returns the ith through jth elements from list.
lappend listVar arg arg ... Append a elements to the value of listVar.
linsert list index arg arg .. Insert elements into list before position index. Returns a new list.
lreplace list i j arg arg ... Replace elements i through j of list with the args. Returns a new list.
lsearch mode list value Return the index of the element in list. Return -1 if not found.
lsort switches list Sort elements of the list. Returns a new list.
concat arg arg arg ... Join multiple lists together into one list.
split string splitChars Split a string up into list elements.
Notes : You can create list by using group like {} (without using command list)
Example: set listA 01 02 03
set listA {01 02 03}
set listA [list 01 02 03]
Notes: The lappend command is unique among the list-related commands because its first
argument is the name of a list-valued variable, while all the other commands take list
values as arguments.
Example: llength $listA
lappend listA 04
Data Types - Array
The other primary data structure that Tcl has is arrays. An array is a variable with a string-
valued index, so you can think of an array as a mapping from strings to strings :
set array1(index1) value1
set array1(index2) value2
Some basic array commands:
Syntax Description
array exists arr Returns 1 if arr is an array variable.
array get arr Returns a list that corresponding array.
array names arr Return the list of all indices defined for arr
array size arr Return the number of indices defined for arr.
Example:
% set arr(idx1) 12345
12345
% set arr(idx2) HelloTcl
HelloTcl
% array exist arr
1
% array get arr
idx1 12345 idx2 HelloTcl
% array names arr
idx1 idx2
% array size arr
2
Flow Control
Decision (if, elseif, else)
Loops (for, foreach, while, break,
continue)
Exceptions (try, except)
Flow Control - Decision
if boolean body1 elseif body2 else body3
% set x -6
-6
% if {$x > 0} {
puts Positive
} elseif {$x == 0} {
puts Zero
} else {
puts Negative
}
Negative
%
Flow Control Loops (1)
for initial test next body
(Notes: Use incr command for next)
% set i 4
4
% while {$i<6} {
puts "i = $i
incr i
}
i = 4
i = 5
%
[Notes]
break : causes immediate exit from a loop
continue : command causes the loop to continue with the next iteration.
Procedure
syntax, scope
Function - Syntax
[Define] proc name params body
% proc sum { a b } {
puts "$a + $b = [expr $a+$b]
}
%
% sum 5 10
5 + 10 = 15
%
Function - Scope
Local scope:
-Each procedure has a local scope for variables.
-Variables defined outside the procedure are not visible to a procedure.
Global Scope
- For variables defined outside the procedure be able to visible outside use global
command
http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html
http://www.beedub.com/book/
Practical Programming in Tcl and Tk (book).pdf
- The End -
Thank You