Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
329 views

TCL Basics

The document provides an overview of TCL basics including TCL script structure, variables, substitution, calculations, procedures and file manipulation. It discusses TCL commands, data types, control flow, strings, lists, arrays and regular expressions.

Uploaded by

Agnathavasi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
329 views

TCL Basics

The document provides an overview of TCL basics including TCL script structure, variables, substitution, calculations, procedures and file manipulation. It discusses TCL commands, data types, control flow, strings, lists, arrays and regular expressions.

Uploaded by

Agnathavasi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

TCL Basics

Kamakhya Prasad Sahu

Intel Confidential
1 Intel Confidential Intel Labs (STPL/BDL)
Agenda
• Introduction
• TCL Script
• TCL Basics
• TCL Control Flow
• TCL String
• TCL List
• TCL Array
• File I/O and Program Access
• Regular Expression
• Advanced TCL scripting

2 Intel Confidential Intel Labs (STPL/BDL)


Introduction
• TCL stands for Tool Command Language. a.tcl
• TCL interpreter can be embedded with other tools.
• Purely Interpreted Language [unix]% tclsh a.tcl
• Extensions of TCL : Tk, Ora Tcl, Incr Tcl. p : 10
missing "
• Command line interface. while executing
"puts "i : $i
• Open source and very easy to learn.
"
a.c (file "a.tcl" line 6)
[unix]%

[unix]% gcc a.c


a.c: In function ‘main’:
a.c:10:1: error: expected ‘;’ before ‘}’ token
[unix]%

3 Intel Confidential Intel Labs (STPL/BDL)


Executing a TCL Script

[unix]$ tclsh hello_world.tcl


Hello World!
[unix]$

[unix]$ tclsh
% puts “Hello World!”
Hello World!
% source hello_world.tcl
Hello World!
% exit
[unix]$

4 Intel Confidential Intel Labs (STPL/BDL)


Writing a TCL Script
• TCL Consists of commands and their arguments.
• Single line comment starts with “#”.
• Muliti-line commands “;” should be used.
• TCL is case sensitive. [unix]$ tclsh
% puts “Hello World!”; #it's a comment
• Escape character is “\”. Hello World!
• Executes command by command. % # single line comment
% set a 5; puts “$a”;
5
% !!
set a 5; puts “$a”;
5
% set A “Hi”; puts \
“$A”;
Hi
%

5 Intel Confidential Intel Labs (STPL/BDL)


Basics of TCL
• TCL Consists of commands and their arguments.
• Execution of TCL script.
[unix]$ tclsh
- Commands Separation % puts "hello TCL"
- Grouping hello TCL
- Substitution % puts "hello
TCL"
- Evalution hello
• Difference between “” and {}. TCL
% puts "Hello \nTCL"
• “”/{} inside {} and {} inside “” treated Hello
as ordinary character. TCL
% puts "hello"; puts "TCL"
• “” inside “” are pair matched hello
TCL
%

6 Intel Confidential Intel Labs (STPL/BDL)


TCL Variables
[unix]$ tclsh
• TCL is typeless. It stores variables as % set a "hello TCL"
strings. hello TCL
• Command “set” is used to assign or % set b
can't read "b": no such var
retrieve the value for a variable. % set a
• Command “unset” is to delete one or hello TCL
% unset a
more variables. % set $@!# "hello TCL"
• Variable name supports any character. hello TCL
% set $@!#
Numerical Values hello TCL
- Integers : 123, 0x7B, 0163 % set octal 0125
- Real numbers: 3.123, 1.23E26, 123.0 0125
% set reg 0x3e
- Boolean: 1, 0 0x3e
% set num 1.23e+2
1.23e+2

7 Intel Confidential Intel Labs (STPL/BDL)


Substitution
[unix]$ tclsh
• Different kinds of substitution % set n 5 ; puts "n = $n"
n = 5
- Variable % puts "n = $n ns"
- Backslash n = 5 ns
- Command % puts "n = ${n}ns"
n = 5ns
• “$” is used for variable substitution. % puts "\$n = $n\tns"
$n = 5 ns
• Use of “${variable}”. % puts "n = $$n"
• Backslash substitution. n = $5
% puts "Done !!! \a\a\a"
\n, \t, \b, \a Done !!!
\x5c, \x7B, \117 % puts "ABC = \x41\102\x43"
\\, \$,\”, \ ABC = ABC
% puts "n = [ set n ]"
• Command substitution “[ ]” N = 5
% puts [set b "5 [set a "ns"]"]
5 ns

8 Intel Confidential Intel Labs (STPL/BDL)


Calculation
[unix]$ tclsh
• Different kinds of substitution % set a 10; set b 5;
5
- Variable % expr $a - $b
- Backslash 5
- Command % expr {$a - $b}
5
• “$” is used for variable substitution. % set a 0x07
0x07
• Use of “${variable}”. % expr $a & 0xe
• Backslash substitution. 6
% set pi 3.14
\n, \t, \b, \a 3.14
\x5c, \x7B, \117 % expr sin($pi/3)
\\, \$,\”, \ 0.8657598394923444
% set tcl_precision 4
• Command substitution “[ ]” 4
% expr sin($pi/3)
0.8658

9 Intel Confidential Intel Labs (STPL/BDL)


Procedures
[unix]$ tclsh
• “proc” command for readability % proc sum {a b } {
return [ expr $a + $b ]
and code reuse. }
proc name args body % set c [ sum 4 5 ]
• Use of “global” inside procedure. 9
% set a "hello TCL"
• Basic File manipulation commands hello TCL
% proc printsub { } {
- glob global a ; puts $a
- pwd }
% printsub
hello TCL
% set a [ pwd ]
/home/kpsahu5
% puts $a
/home/kpsahu5
% glob /home/kpsahu5/*.tcl
/home/kpsahu5/hello_world.tcl

10 Intel Confidential Intel Labs (STPL/BDL)


Quiz
• Output of the following code.
set a 2; set b 4; set c 5;
proc edit { } {
global a
set a 6; set ::c 7;
}
edit
puts “a: $a, b: $b, c: $c” a: 6, b: 4, c: 7

• Output of the following code.


set max 34
set a [ expr int ( rand() + 1 ) * $max ]
puts “a: $a” a: 34

11 Intel Confidential Intel Labs (STPL/BDL)


Conditional [unix]$ tclsh
% set cel AND2
• If, if-else and if-elseif-else % switch -- $cel {
• Switch AND { puts “AND!!!” }
OR { puts “OR!!!” }
-glob default { puts “not found” }}
-regexp not found
-exact % switch -- $cel {
AND2 - AND3 { puts “AND!” }
-- OR { puts “OR” }
default { puts “not found” }}
[unix]$ tclsh AND!
% set a 20 % switch -glob -- $cel {
20 AND* { puts “AND!!!” }
% if { $a > 20 } { puts "$a > 20" default { puts “not found” }}
} elseif { $a < 20 } { puts "$s < 20" AND!!!
} else { puts "$a = 20" % switch -regexp -- $cel {
} {AND\d+} { puts “AND!!!” }
20 = 20 default { puts “not found” }}
% AND!!!

12 Intel Confidential Intel Labs (STPL/BDL)


Loop
• For Loop [unix]$ tclsh
% set sum 0
• Foreach Loop 0
• While Loop % for {set i 1} {$i <= 7} {incr i} {
set sum [ expr $sum + $i ]
}
[unix]$ tclsh % puts "Sum = $sum"
% set i 1 Sum = 28
1 % set sum 0
% set sum 0 0
0 % set a {1 2 3 4 5 6 7}
% while { $i <= 7 } { 1 2 3 4 5 6 7
set sum [ expr $sum + $i ] % foreach b $a {
incr i set sum [ expr $sum + $i ]
} }
% puts “Sum = $sum” % puts “Sum = $sum”
Sum = 28 Sum = 28
% %

13 Intel Confidential Intel Labs (STPL/BDL)


Loop Control
[unix]$ tclsh
• break to terminate % set sum 0
• continue to advance the loop 0
% for {set i 1} {$i <= 7} {incr i} {
set sum [ expr $sum + $i ]
if { $i == 5 } { break }
}
[unix]$ tclsh
% puts "Sum = $sum"
% set sum 0 Sum = 15
0
% set sum 0
% for {set i 1} {$i <= 7} {incr i} {
0
set sum [ expr $sum + $i ]
% for {set i 1} {$i <= 7} {incr i} {
if { $i == 5 } { continue } if { $i == 5 } { continue }
}
set sum [ expr $sum + $i ]
% puts "Sum = $sum" }
Sum = 28
% puts "Sum = $sum"
Sum = 23

14 Intel Confidential Intel Labs (STPL/BDL)


String
[unix]$ tclsh
• Appending strings using append % set a hello; set b world;
world
• Comparing strings using string compare % set c "$a ${b}!"
and “==” hello world!
% append var $a " " $b "!"
• String matching using string match hello world!
% expr "0x03" == "0003"
1
% string compare "0x03" "0003"
[unix]$ tclsh 1
% set a {p q r} % string compare "0003" "0x03"
p q r -1
% foreach x $a { % string compare "0x03" "0x03"
append b "set " $x " 0; " 0
} % string match "*/abc/*" "xyz/abc/A4"
% puts $b 1
set p 0; set q 0; set r 0; % string match "*/abc/*" "pqr/ab/A4"
% 0

15 Intel Confidential Intel Labs (STPL/BDL)


String Related Commands
• toupper, tolower
[unix]$ tclsh
• trim, trimleft, trimright % string toupper "Hello World!"
HELLO WORLD!
• first, last, length % string tolower "HELLO WORLD!"
hello world!
% string trim " hello "
[unix]$ tclsh hello
% set a “abcdefghijabcde” % string trim "(((a+b)))" "()"
abcdefghijabcde a+b
% string first "de" $a % string trim "(((a+b)))" ")("
3 a+b
% string last "de" $a % string trimright "../abc/./pqr/*" "./*"
13 ../abc/./pqr
% string length $a % string trimleft "../abc/./pqr/*" "./*"
15 abc/./pqr/*

16 Intel Confidential Intel Labs (STPL/BDL)


String Related Commands
[unix]$ tclsh
• Index, range % set a “abcdefghijabcdefghij”
abcdefghijabcdefghij
• Repeat % string index $a 2
• relpace, map c
% string range $a 10 12
• Is upper abc
% string range $a 10 end
abcdefghij
% string repeat [string range $a 0 2] 3
abcabcabc
% string map {abc 3 de 2} $a
32fghij32fghij
% string replace $a 10 end "klmn"
Abcdefghijklmn
% set a "HI"; string is upper $a;
1
% set a "Hi"; string is upper $a;
0

17 Intel Confidential Intel Labs (STPL/BDL)


Quiz
• Write a code to extract data from abc to abc in variable a
set a “abcdefghijabcdefghij”;
Required output : defghij

• Output of the following code.


set msg “vyoe orgd”
set a “0 3 6”
set j 0
while {$j < 3} {
foreach x $a {
append ans [string index $msg [ expr $x + $j ] ]
}
incr j
}
puts “ans : $ans”

18 Intel Confidential Intel Labs (STPL/BDL)


[unix]$ tclsh
List % set a “a b c”
a b c
% lindex $a 1
• Creating a list b
• llength, lindex, lrange % set x [ list $a d e ]
{a b c} d e
• lappend and concat % set y “$a d e”
• split and join a b c d e
% llength $y
5
% lindex [ lindex $x 0 ] 1
[unix]$ tclsh b
% set a "abc/pqr/xyz" % lappend y f g
abc/pqr/xyz a b c d e f g
% set b [ split $a "/" ] % concat $y $x
abc pqr xyz a b c d e f g {a b c} d e
% set c [ join $b "::" ] % set z [ list $y $x ]
abc::pqr::xyz {a b c d e f g} {{a b c} d e}
% set a $c % lrange $y 2 5
abc::pqr::xyz c d e f

19 Intel Confidential Intel Labs (STPL/BDL)


Editing List
• linsert and lreplace
• List and String [unix]$ tclsh
% set a "TCL easy"
[unix]$ tclsh TCL easy
% set a "a b c d e f g h i j" % puts $a
a b c d e f g h i j TCL easy
% lreplace $a 3 3 D % puts [ lindex $a 0 ]
a b c D e f g h i j TCL
% lreplace $a 3 4 D E % linsert $a 1 "is"
a b c D E f g h i j TCL is easy
% puts $a % linsert $a end-1 "very"
a b c d e f g h i j TCL very easy
% lreplace $a 3 4 D E F % set a [ linsert $a 1 "is" ]
a b c D E F f g h i j TCL is easy
% set a [ lreplace $a 3 4 D E F ] % set a [ linsert $a end-1 "very" ]
a b c D E F f g h i j TCL is very easy
% set a [ lreplace $a 5 5 ] % puts $a
a b c D E f g h i j TCL is very eas

20 Intel Confidential Intel Labs (STPL/BDL)


Searching and Sorting in List
• lsearch (returns -1 or index of first element of pattern) -exact, -glob, -regexp
• lsort and -command, -decreasing, -integer, -ascii, -increasing, -dictionary
[unix]$ tclsh
% proc comp {a b } { expr $a - $b } [unix]$ tclsh
% set a "45 3 67 31 2 134 1 14" % set a "abc ab xyz xab pqr"
45 3 67 31 2 134 1 14 abc ab xyz xab pqr
% lsort $a %lsearch $a pq*
1 134 14 2 3 31 45 67 4
% lsort -command comp $a % lsearch -glob $a pq*
1 2 3 14 31 45 67 134 4
% lsort -integer $a % lsearch -exact $a ab
1 2 3 14 31 45 67 134 1
% lsort -decreasing $a % lsearch -regexp $a {\w\wb}
67 45 31 3 2 14 134 1 3
% lsort -decreasing -integer $a % lsearch -regexp $a "\w\wb"
134 67 45 31 14 3 2 1 -1

21 Intel Confidential Intel Labs (STPL/BDL)


Array
• Creating and manupulating array
• Multi-dimensional array
[unix]$ tclsh
% array set a_array {a abc p pqr x xyz}
% puts $a_array(a)
abc
% set a_array(m) mno
mno
% unset a_array(p)
% array get a_array
x xyz m mno a abc
% set image(0,4) 23
23
% set image(255,255) 24
24

22 Intel Confidential Intel Labs (STPL/BDL)


Array Related Commands
• exists, size, names
• list and array

[unix]$ tclsh
[unix]$ tclsh % array get a_array
% array get a_array x xyz m mno a abc
x xyz m mno a abc % set a [ array get a_array ]
% array exist a_array x xyz m mno a abc
1 % lindex $a 2
% array size a_array m
3 % array set b_array $a
% array names a_array % array get b_array
x m a x xyz a abc m mno

23 Intel Confidential Intel Labs (STPL/BDL)


Quiz
• Write a code to reverse the elements of a list
set a “a b c d e”; # Required output $a = e d c b a

• Output of the following code.


set a_list “a b c d e”
array set a_array “0 a 1 b 2 c 3 d 4 e”
puts “ list : [ lindex $a_list 5 ]”
puts “ array: $a_array(5)”

• Write to sort the cell name depending upon the hierarchy


set a “abc abc/xyz abc/pqr/mno abc/pqr”
# Required output “abc/pqr/mno abc/pqr abc/xyz abc ”

24 Intel Confidential Intel Labs (STPL/BDL)


File Handling
[unix]$ tclsh
• Opening and closing file % proc a { fid } {
Read only (default) r while {[gets $fid line ] >= 0 } {
Read/write (exists) r+ puts "$line "
}}%
Write w set fid [ open "hello.txt" r ]
Write/read w+ file5
Append a % a $fid
hello
Append/read a+ world!!!
• gets, eof % set fid [ open "abc.txt" w ]
file5
• File (file mkdir, file copy, file rename ) % puts $fid "hello "
% puts -nonewline $fid "world"
% puts $fid "!!!"
% close $fid
1

25 Intel Confidential Intel Labs (STPL/BDL)


File Handling
• File Related Commands
file mkdir, file copy, file rename, file exists, file dirname, file tail

[unix]$ tclsh
% file rename abc.txt hello.txt
[unix]$ tclsh % proc b { fid } {
% file dirname /nfs/iind/home/kpsahu5/run_apr.csh while { ![eof $fid ] } {
/nfs/iind/home/kpsahu5 gets $fid line; puts $line;
% file tail /nfs/iind/home/kpsahu5/run_apr.csh } }
run_apr.csh % set fid [ open "hello.txt" r ]
% file exists /nfs/iind/home/kpsahu5/run_apr.csh file6
1 % b $fid
% exit hello
world!!!

26 Intel Confidential Intel Labs (STPL/BDL)


External Commands
• exec [unix]$ tclsh
% glob *.tk
• auto_noexec a.tk b.tk
• eval (removes the scope) % exec ls -lrt [ glob *.tk ]
ls: a.tk b.tk: No such file or directory
% eval exec ls -lrt [ glob *.tk ]
-rw-rw-r-- 1 ksahu ksahu 48 Apr 23 01:10 a.tk
-rw-rw-r-- 1 ksahu ksahu 64 Apr 23 01:10 b.tk
% cat anc.txt
hello
world%
% grep hello anc.txt
hello
% set auto_noexec ""
% grep hello anc.txt
invalid command name "grep"
% exec grep hello anc.txt
hello

27 Intel Confidential Intel Labs (STPL/BDL)


Regular Expression Examples
Example Description
[Pp]ython Match "Python" or "python"
rub[ye] Match "ruby" or "rube"
[aeiou] Match any one lowercase vowel
[0-9] Match any digit; same as [0123456789]
[a-z] Match any lowercase ASCII letter
[A-Z] Match any uppercase ASCII letter
[a-zA-Z0-9] Match any of the above
[^aeiou] Match anything other than a lowercase vowel
[^0-9] Match anything other than a digit
. Match any character except newline
\d Match a digit: [0-9]
\D Match a nondigit: [^0-9]
\s Match a whitespace character: [ \t\r\n\f]
\S Match nonwhitespace: [^ \t\r\n\f]
\w Match a single word character: [A-Za-z0-9_]
\W Match a nonword character: [^A-Za-z0-9_]
ruby? Match "rub" or "ruby": the y is optional
ruby* Match "rub" plus 0 or more y’s
ruby+ Match "rub" plus 1 or more y’s

28 Intel Confidential Intel Labs (STPL/BDL)


Regular Expression Examples
Example Description
\b Matches word boundaries
\B Matches non-word boundaries
\d{3} Match exactly 3 digits
\d{3,} Match 3 or more digits
\d{3,5} Match 3, 4, or 5 digits
\D\d+ No group: + repeats \d
(\D\d)+ Grouped: + repeats \D\d pair
([Pp]ot(, )?)+ Match "Pot", "Pot, pot, pot"etc.
python|perl Match "python" or "perl"
rub(y|le)) Match "ruby" or "ruble"
Python(!+|\?) "Python" followed by one or more ! or one ?
^Python Match "Python" at the start of a string/line
Python$ Match "Python" at the end of a string/line
\APython Match "Python" at the start of a string
Python\Z Match "Python" at the end of a string
\bPython\b Match "Python" at a word boundary
\brub\B Match "rub" in "rube" and "ruby" but not alone
Python(?=!) Match "Python", if followed by one !
Python(?!!) Match "Python", if not followed by one !

29 Intel Confidential Intel Labs (STPL/BDL)


regexp and regsub
[unix]$ tclsh
% regexp {[A-Z][A-Z]*} "VHDL or Verilog" var
1
% puts $var
VHDL
% regexp -nocase -- {[A-Z][A-Z]*} "Verilog or VHDL" var
1
% puts $var
Verilog
% regexp -indices -- {[A-Z][A-Z]*} "VHDL or Verilog" var
1
% puts $var
0 3
% string range "VHDL or Verilog" [lindex $var 0 ] [lindex $var end ]
VHDL
% regsub -- {[a-z]+} "cin : 10013" "carry_in" new_string
1
% puts $new_string
carry_in : 10013

30 Intel Confidential Intel Labs (STPL/BDL)


Introduction to TCL-TK

calc.tk

[unix]% wish calc.tk proc abc { a } {


if {$a == ""} {
.c configure -text "Answer :"
} else {
.c configure -text "Answer : [ expr $a ]"
}
}
set ans ""
wm title . "CALCULATOR"
button .b -text " ANSWER " -command {abc $var}
grid .b -row 1 -column 0 -padx 10 -pady 5
entry .a -textvariable var
grid .a -row 0 -column 0 -padx 10 -pady 5
label .c -text ""
grid .c -row 2 -column 0 -padx 10 -pady 5

31 Intel Confidential Intel Labs (STPL/BDL)


THANK YOU

32 Intel Confidential Intel Labs (STPL/BDL)

You might also like