TCL Script
TCL Script
Features
output:
Hello, World!
Comment:
#!/usr/bin/tclsh
# my first program in TCL
puts "Hello World!"
Variable Substitution
$ is used before the variable name and this returns the contents of the variable.
Ex: #!/usr/bin/tclsh
set a 3
puts $a ……………….result: 3
Backslash Substitution
These are commonly called escape sequences; with each backslash, followed by a letter having its
own meaning.
Ex: #!/usr/bin/tclsh
puts "Hello/nWorld“ ……………….result: Hello
World
Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations.
TCL language is rich in built-in operators and provides the following types of operators:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Ternary Operator
Arithmetic Operators
The arithmetic operators perform addition (+), subtraction (-), multiplication (*), division (/),
and modulus(%) operations
Ex:- set a 20
set b 10
puts [expr $a + $b] ……………Result: 30
puts [expr $a - $b] ……………Result: 10
puts [expr $a * $b] ……………Result: 200
puts [expr $a / $b] ……………Result: 2
puts [expr $a % $b] ……………Result: 0
Relational Operators
Used to check the relational between two operands. If the condition is true
then the value returned is 1 else false return 0
ex: set a 20
set b 10
puts [expr $a > $b] ………………….Result: 1
puts [expr $a < $b] ………………….Result: 0
puts [expr $a >= $b]
………………….Result: 1
puts [expr $a <= $b] ………………….Result: 0
puts [expr $a != $b]
………………….Result: 1
puts [expr $a == $b] ………………….Result: 0
Logical Operators
• && If both the operands are non-zero, then condition becomes true and return 1 else return 0
ex: set a 20
set b 10
puts [expr $a && $b] …………………….Result: 1
• || If any of the two operands is non-zero, then condition becomes true and return 1 else return
0
ex: set a 0
set b 10
puts [expr $a || $b] …………………….Result: 1
• ! Used to reverse the result of any expression(only for single bit)
ex: set a 0
set b 1
puts [expr !$a] ] …………………….Result: 1
puts [expr !$b] ] …………………….Result: 0
Bitwise Operator
Bitwise operator works on bits and perform bit-by-bit operation.
• & (bitwise and)
• | (bitwise or)
• ^ (bitwise exclusive or)
• ~ (bitwise negation)
Ex:- set a10 #10 in bin-
00001010
set b 20 # 20 in bin-
00010100
puts [expr $a & $b] ……………Result: 0 0 0 0 0 0 0 0
puts [expr $a | $b] ........………Result: 0 0 0 1 1 1 1 0
puts [expr $a ^ $b] .…………….Result: 0 0 0 1 1 1 1 0
set c 7
puts [expr ~$c] ….………….Result: -8
Ternary Operator ( ?:)
Syntax : condition-expression ? expression_1 : expression_2
ex: set A 7
set result [expr $A > 6 ? true : false]
puts $result …………………….. result : true
Decisions
• Decision making structures require that
the programmer specifies one or more
conditions to be evaluated or tested by
the program
• if statement
• if...else statement
• nested if statements
• switch statement
Syntax:
if {boolean_expression/condition} {
# statement(s) will execute if the Boolean expression is true
}
Ex: set a 10
Syntax: set b 20
if {$a == 10} {
If {expression_1} {
if {$b == 20} {
Body_1 puts "value of a is 10 and b is 20"
If {expression_2} { }
}
Body_2 result: value of a is 10 and b is 20
}
}
Switch Statement Nested switch
Nested switch statement means switch
This enables a variable to be tested for equality against statement inside a switch statement.
a list of values. It evaluates the list and returns the
result of that evaluation. If no values matches then Example:
default values will be returned. set a 100
set b 200
syntax:
switch switchingString { switch $a {
100 {
matchString1 { puts "The value of a is $a"
body1 switch $b {
} 200 {
puts "The value of b is $b"
matchString2 { }
body2 }
} }
}
... result:-
matchStringn { The value of a is 100
bodyn The value of b is 200
}
Loops
• A loop statement allows us to execute a statement or group of
statements multiple times
types of loops
• while loop
• for loop
• foreach loop
• nested loop
Syntax:
while {condition} {
statement(s)
}
Ex: set a 10 result:
value of a: 10
while { $a < =15 } { value of a: 11
puts "value of a: $a" value of a: 12
value of a: 13
incr a value of a: 14
value of a: 15
}
For Loop
A for loop is a repetition control structure that allows you to efficiently write a code that
needs to be executed for a specific number of times.
Syntax:
for {initialization} {condition} {increment} {
statement(s);
}
result:
Ex: value of a: 10
for loop execution value of a: 11
value of a: 12
for { set a 10} {$a < 15} {incr a} { value of a: 13
puts "value of a: $a" value of a: 14
}
Foreach Loop
The foreach command implements a loop where the loop variable(s) take on values from one
or more lists.
syntax:
foreach <object name> <collection of objects> {
body
}
Ex: result:
15aw
foreach a [list 1 2 3 4] b [list 5 6 7 8] c [list a b c d] d [list w x y z] { 26bx
puts "$a $b $c $d" 37cy
48dz
}
Nested Loops
TCL allows to use one loop inside another loop.
Ex: Find the prime numbers from 2 to 50:
Syntax: set j 0;
for {set i 2} {$i<100} {incr i} {
for {initialization} {condition} {increment} { for {set j 2} {$j <= [expr $i/$j] } {incr j} {
for {initialization} {condition} {increment} {
if { [expr $i % $j] == 0 } {
statement(s); break
}
} }
statement(s); if {$j >[expr $i / $j] } {
puts "$i "
} }
}
result: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
Loop Control Statements
Loop control statements change execution from its normal sequence.
Break Statement:
The break statement in TCL language is used for terminating a loop.
Syntax:
break;
Ex: set a 10
while {$a < 20 } { result:
value of a: 10
puts "value of a: $a" value of a: 11
incr a value of a: 12
value of a: 13
if { $a > 15} {
value of a: 14
break # terminate the loop using break value of a: 15
statement
}
Continue Statement:
• The continue statement in TCL language works somewhat like the break statement.
Instead of forcing termination,
• however, continue forces the next iteration of the loop to take place, skipping any code
in between.
syntax:
continue;
Ex: set a 10
while { $a < 15 } {
result:
if { $a == 12} { value of a: 10
incr a value of a: 11
value of a: 12
continue #skip the iteration value of a: 13
} value of a: 14
Size of Array
syntax: [array size variablename]
Ex: set languages(0) TCL result:
set languages(1) "C Language" 2
puts [array size languages]
Strings
A string is a sequence of characters. Strings can contain alphanumeric character, just numbers,
Boolean, or even binary data.
Ex: set myVariable "hello world"
result: hello world
puts $myVariable
string index
Returns the character at index
Ex: set s1 "Hello World"
set s2 "o"
puts "Character at index 0 in s1“
result: Character at index 0 in s1 H
puts [string index $s1 0]
Length of String:
Returns a decimal string giving the number of characters in string
Ex: set s1 "Hello World"
puts "Length of string s1" result:
puts [string length $s1] Length of string s1 11
string match
Returns 1 if the string matches the pattern.
Ex: set s1 "test1.test.com"
set s2 ".com"
puts "Matching pattern s2 in s1"
result:
puts [string match ".com" $s1 ] Matching pattern s2 in s1 1
String Comparison:
Perform a character-by-character comparison of strings string1 and string2 if equal value returned is 0
else 1
Ex: puts [string compare 12 12] Result: 0
Append Command
Used to append at the end of the existing string
Syntax: append string string
Ex: set s1 "Hello"
append s1 " World"
puts $s1 Result: Hello World
String Commands
• string range: Returns a range of consecutive characters from string
• string tolower: Returns a value equal to string except that all upper case letters have been converted to
lower case
• string toupper: Returns a value equal to string except that all lower case letters have been converted
to upper case.
• string wordend: Returns the index of the character just after the last one in the word containing
character index of string
• string wordstart: Returns the index of the first character in the word containing character index of
string.
• string last: last string1 string2 : Returns the index last occurrence of string1 in string2. If not found,
returns -1.
Lists
• List is one of the basic data-type available in TCL.
• It is used for representing an ordered collection of items.
Creating a List
Syntax:
set listName { item1 item2 item3 .. itemn }
# or set listName [list item1 item2 item3]
Dictionary Iteration
A simple dictionary iteration for printing keys and valued of the dictionary
Ex: set colours [dict create colour1 "black" colour2 "white"]
foreach item [dict keys $colours] {
set value [dict get $colours $item]
puts $value result: black
} white
Value for Key in Dict
Syntax: [dict get $dictname $keyname]
Ex: set colours [dict create colour1 "black" colour2 "white"]
set value [dict get $colours colour1]
puts $value result: black
if {$number <= 1} {
return 1
Closing a File
To close a file, use the close command.
Syntax: close fileName
Writing a File
Puts command is used to write to an open file.
puts $filename "text to write"
Ex: set fp [open "input.txt" w+]
puts $fp "test"
close $fp
Reading a File
the simple command to read from a file:
Ex: #!/usr/bin/tclsh
close $fp
puts $file_data
• line: New line sensitive matching. Ignores the characters after newline.
# replace the data in the same string; all matches are replaced
regsub -all {(yellow|blue)} $mydata red mydata
puts $mydata result: The red dog has the reds
Thank You….!