VTA Training Course: Topics
VTA Training Course: Topics
VTA Training Course: Topics
Topics:
➢Introduction
➢Variable declaration
➢Data type
➢Operator
➢Array
➢List
➢String
➢Decisions
➢Loops
➢Procedures
➢Regular expression
➢File Handling
Introduction
output:
Hello World
Hello World without semicolon
### Semicolon ‘;’ is also significant if you want to put comment after the end of
statement.
puts “hello world” #comment
Above statement will throw syntex error
Variable declaration
Input:
Set str “this is string”;
Set value 1.5;
puts “$str”;
puts “the value is $value”;
output:
this is string
this is value 1.5
Dollar ‘$’ is used as prefix with variable to print variable value in command line.
Square braces ‘[]’ are used for assigning value to variable from other expressions.
set a “5”;
set b $a; # this statement will assign b to a
Below statement will also assign b to a but in single statement
set b [set a “5”];
It will take statement inside square braces, evaluate it, and, assign output of it to
the variable b.
output:
string in double quotes
string in curly braces
Why the curly brace ‘{‘ is required when double quote is already there for
printing?
Curly braces doesn’t recognize variable. It print all the $variables as it is.
set value 1.5;
puts {the value is $value};
put “the value is $value”
output:
the value is $value
the value is 1.5
set a “5”;
set b “3”;
set c [expr “$a + $b”]; # ”[]” is used to put output of expr statement into variable
c.
puts “addition of a and b is $c”;
output:
addition of a and b is 8
Operators
Arithmetic Operators
Lets A = 5; B = 10
Relational Operators
Let's A = 5; B = 10
> Checks if the value of left operand is greater than (A > B) is not true
the value of right operand, if yes then condition
becomes true.
< Checks if the value of left operand is less than the (A < B) is true
value of right operand, if yes then condition
becomes true.
>= Checks if the value of left operand is greater than or (A >= B) is not true
equal to the value of right operand, if yes then
condition becomes true.
<= Checks if the value of left operand is less than or (A <= B) is true
equal to the value of right operand, if yes then
condition becomes true.
Logical Operators
Let's A = 1; B = 0
Operator Description Example
&& Called Logical AND operator. If both the operands (A && B) is false
are non-zero, then condition becomes true.
|| Called Logical OR Operator. If any of the two (A || B) is true
operands is non-zero, then condition becomes
true.
! Called Logical NOT Operator. Use to reverses the !(A && B) is true
logical state of its operand. If a condition is true
then Logical NOT operator will make false.
Bitwise Operators
Let's A = 50; B = 30
Operator Example
& A&B = 01 0010
| A | B = 11 1110
^ A^B = 10 1100
<< A<<2 = 11 001000
>> A>>2 = 001100
Ternary Operator
?: If Condition is true? Then value X expr { (0 < 1) ? [puts 100] : [puts 50] }
: Otherwise value Y Result: 100
***Example
set a 10;
set b [expr $a == 1 ? 20: 30]
puts "Value of b is $b\n"
set b [expr $a == 10 ? 20: 30]
puts "Value of b is $b\n"
Arrays
***Array Declaration: Array are ordered set of values
The syntax is:
set ArrayName (Index) value
example:
set institute (0) vlsi
set institute (1) training
set institute (2) academy
Output:
Vlsi
Training
Academy
*** Size of Array
The syntex for calculating size array is shown below
[array size variablename]
example:
set institute (0) vlsi
set institute (1) training
set institute (2) academy
output:
3
***Array Iteration
set institute (0) vlsi
set institute (1) training
set institute (2) academy
for { set index 0 } { $index < [array size institute] { incr index } {
puts "institute($index) : $institute($index)"
}
Output:
institute (0) : vlsi
institute (1) : training
institute (2) : academy
List
***Declaring a list
set color {red green blue yellow}
puts $color
output:
red green blue yellow
output:
red green blue yellow
***Length of list
set color {red green blue yellow}
puts [llength $color]
output:
4
***access index
set color {red green blue yellow}
puts [lindex $color 1]
output:
green
***insert at index
set color {red green blue yellow}
set color [linsert $x 3 black white
puts $color
output:
red green blue black white yellow
***replace items at indices ***set items at index ***sorting a list
set x {orange blue red green} set x {orange blue red green} set x {orange blue red green}
set x [lreplace $x 2 3 black white] lset x 0 black set x [lsort $var]
puts $x puts $x puts $x
Result: orange blue black white Result: black blue red green Result: blue green orange red
Strings
***example on string
set myVariable "hello world"
puts $myVariable
set myVariable {hello world}
puts $myVariable
output:
hello world
hello world
\\ \ character
\? ? character
\a Alert or bell
\b Backspace
\n Newline
\t Horizontal tab
\v Vertical tab
output:
First occurrence of o in s1
4
Character at index 0 in s1
H
Last occurrence of o in s1
7
***example on string match ***example on format command
set s1 "[email protected]" puts [format "%f" 43.5]
set s2 "*@*.com" puts [format "%e" 43.5]
puts "Matching pattern s2 in s1" puts [format "%d %s" 4 tuts]
puts [string match "*@*.com" $s1 ] puts [format "%s" “Tafuri Tech"]
puts "Matching pattern tcl in s1" puts [format "%s" “Tafuri\ Tech"]
puts [string match {tcl} $s1] puts [format "%x" 40]
output: output:
Matching pattern s2 in s1 43.500000
1 4.350000e+01
Matching pattern tcl in s1 4 tuts
0 “Tafuri
“Tafuri Tech"
28
output: output:
1 0
1 0
0 0
1 0
Task: Check what will happen if strict
option is not used.
Decisions
*** if statement
The syntax of an 'if...else' statement in Tcl language is –
if {boolean_expression} {
# statement(s) will execute if the boolean expression is true
} else {
# statement(s) will execute if the boolean expression is false
}
***for loop
The syntax of a for loop in Tcl language is –
for {initialization} {condition} {increment} {
statement(s);
}
***for loop
for { set a 10} {$a < 15} {incr a} {
puts "value of a: $a"
}
output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
***while loop
The syntax of a while loop in Tcl language is −
while {condition} {
statement(s)
}
***example on while loop
set a 10
while { $a < 15 } {
puts "value of a: $a"
incr a
}
output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
Basic structure
proc procedureName {arguments} {
body
}
output:
40
***Procedures with Default Arguments
proc add {a {b 100} } {
return [expr $a+$b]
}
puts [add 10 20 ]
puts [add 40]
output:
30
140
Regular expression
Operator Description
***Example 2:
set full [regexp -all -inline {(br\S+)} $res]
output:
[find yourself]
File Handling
Mode Description
r Opens an existing text file for reading purpose and the file must exist. This is the
default mode used when no accessMode is specified.
w Opens a text file for writing, if it does not exist, then a new file is created else
existing file is truncated.
a Opens a text file for writing in appending mode and file must exist. Here, your
program will start appending content in the existing file content.
r+ Opens a text file for reading and writing both. File must exist already.
w+ Opens a text file for reading and writing both. It first truncate the file to zero
length if it exists otherwise create the file if it does not exist.
a+ Opens a text file for reading and writing both. It creates the file if it does not
exist. The reading will start from the beginning, but writing can only be
appended.