TCL Scripting For VLSI Part 2

Part 3: Control Structures and Expressions
If..Else
  • The 'if' command is the basic conditional command in Tcl
  • If an expression is true then execute one command body else execute another command body
  • The second command body (the else clause) is optional
  • The syntax of the command is
  • if expression? body1 ?else? body2
Example: 
set x 10
if { $x>0} {
puts "x is greater than 0";
} else {
puts "x is lesser than 0";
}
Output: x is greater than 0
Switch Command
  • The switch command is used to branch to one of many bodies depending on the value of an expression
  • The general form of the command is :
  • switch flags value pattern1 body1 pattern2 body2 ...
  • any number of pattern body pairs can be specified
  • all the pattern body pairs can be grouped into one argument
  • switch flags value {pat1 body1 pat2 body2...}
Example:
set vlsi B;
switch $vlsi {
   A {
      puts "Good"
   }
   B {
      puts "Excellent"
   }
   C {
      puts "Not good"
   }
   default {
      puts "Invalid result"   }
}
Output: Excellent
  • In the switch command there are four possible flags that determine how value is matched
  • -exact: matches the value exactly to one of the patterns
  • --glob: uses glob-style pattern matching (using wildcards like *,?)
  • -regexp: uses regular expression pattern matching
For Loop
  • The for command is a looping construct used to do operations repetitively.
  • The syntax is:
  • for (initialization} {condition} {increment} { 
  • statement(s);
  • }
The for command takes 4 parameters:
  • The first argument is a command to initialize the loop
  • The second argument is a Boolean expression that determines whether the loop body will execute
  • The third argument is a command to increment the value of the variable executed after the loop body
  • The fourth argument is the body of the loop
Example:
 # for loop execution
for { set i 10} {$i < 20} {incr i} { 
puts "value of a: $i"
}
Output:
value of i: 10
value of i: 11
value of i: 12
value of i: 13
value of i: 14
value of i: 15
value of i: 16
value of i: 17
value of i: 18
value of i: 19
While Loop
  • while loop can also be used as a looping construct to execute operations repetitively.
  • The syntax is:
while (condition} { 
 statement(s)
   }
Example: 
set i 0 
while {$i < 10} { 
incr i 
puts $i 
}
Output: 1 to 10
Foreach Loop
  • foreach takes three arguments
  • - the first is the name of a variable
  • - the second is a list
  • - the third is a Tcl script that forms the body of the loop
  • foreach will execute the script once for each element of the list in order
set i 1
foreach value { 1 2 3 4 5  } {  
set i [expr $i+ $value] 
}
puts $i
Output: 16
Break and Continue Statements
  • The break command causes the innermost enclosing looping command to terminate immediately
  • The continue command causes only the current iteration of the innermost loop to be terminated, the loop continues with the next iteration
Example:
Break:
set a 11
while {$a < 20 } {
   puts "value of a: $a"
   incr a
   if { $a > 14} {
      break
   }
}
Output: 
value of a: 11
value of a: 12
value of a: 13
value of a: 14
Continue:
set a 11
while { $a < 15 } {
   if { $a == 15} {
      incr a
      continue
   }
   puts "value of a: $a"
   incr a     
}
Output:
value of a: 11
value of a: 12
value of a: 13
value of a: 14
Expr Command
  • Expressions cannot be evaluated by the Tcl interpreter
  • The expr command is used to parse and evaluate expressions in Tcl
  • It receives the expression as its argument and returns the result after evaluation
Example:
set a [expr 10+20]  # stores expr result in 'a '
puts $a
Output: 30
Operators in TCL
There are basically 5 types of operators in the TCL. They are as follows:
1. Arithmetic Operator
2. Relational Operator
3. Logical Operator
4. Bitwise Operator
5. Ternary Operator
Part 4: String
  • Unlike other languages, in Tcl, you need not include double quotes when it's only a single word. An example can be -
set a hello 
puts $a
When the above code is executed, it produces the following result -
hello
When we want to represent multiple strings, we can use either double quotes or curly braces. It is shown below
set a "hello world"
puts $a
set a {hello world}
puts $a
When the above code is executed, it produces the following result -
hello world
hello world
Format Command
  • The format command is similar to the C printf function
  • It formats a string according to a format specification
  • The syntax of the format command is
  • format spec value1 value2...
  • The spec argument includes literals and keywords
Examples for format:
set x  4 
set y  5
puts [format "In octal: %0o" [expr $x+$y]] 
puts [format "In hexadecimal: %0x" [expr $x+$y]] 
puts [format "In decimal: %d" [expr $x+$y]]
  • The literals are placed in the result as it is
  • The keyword indicates how to format the corresponding argument
  • The keywords are introduced with percent sign % followed by zero or more modifiers and terminate with a conversion specifier
  • Example keywords include %f for floating point, %d for integer, and %s for string format
  • puts [ format "The square root of 10 is %3f" [expr sqrt (10)]]
  • Output: The square root of 10 is 3.162
Scan Command
  • The scan command parses a string according to a format specification and assigns values to variables
  • The syntax of the command is:
  • scan string format var var var...
  • It returns the number of successful conversions it made
String Command
  • The string command is a collection of operations one can perform on strings
  • The first argument to string determines the operation
String Compare: 
% string compare "vlsi" "vlsi"
0
% string compare "vlsi" "4freshers"
-1
% string compare "vlsi4freshers" "vlsi"
1
String equal
  • The syntax of the command is
  • string equal ?-nocase ? str1 str2
  • Compares strings and returns 1 if they are the same or 0 otherwise
  • If -nocase is used it makes a case insensitive comparison
  • puts [string equal -nocase "VLSI" "vlsi"]
  • Output: 1
String First
  • The syntax of the command is
  • string first str1 str2
  • Returns the index in str2 of the first occurrence of str1
  • Returns -1 if str1 is not found
  • puts [string first "vlsi" "abcdvlsi"]
  • index value of occurrence of 'vlsi' in the second string)
  • output: 4
String last
  • The syntax of the command is
  • string last str1 str2
  • Returns the index in str2 of the last occurrence of str1
  • Returns -1 if str1 is not found
String Index
  • The syntax of the command is
  • string index string index
  • Returns the character at the specified index. If index is less than 0 or greater than or equal to the length of the string, an empty string is returned.
  • An index counts from 0. Use keyword 'end' for the last character
  • puts [string index "vlsi" 2]
  • output: s
String Length
  • The syntax of the command is
  • string length string
  • Return the number of characters in the string
  • puts [ string length "vlsi"]
  • output: 4
String match
  • The syntax of the command is
  • string match pattern str
  • Returns 1 if str matches the pattern else 0.
  • When there is the need for more powerful string matching capabilities, string match can be used in place of string equal, because instead to compare two strings, the command compares a string against a pattern.
  • % string match {[0-9]} 4. Output: 1
  • % string match vlsi* vlsi. Output: 1
String range
  • The syntax of the command is
  • string range str i j
  • returns the range of characters in str from i to j
  • puts [ string range "vlsi4freshers" 1 3]
  • Output: lsi 
append: This command is used to append the string value to any string.
Example:
set a "vlsi"
append a 4freshers
puts $a
vlsi4freshers
String replace
  • Returns a new string created by replacing characters first through last with newstr or nothing.
String tolower
  • Returns string in lowercase.
String toupper
  • Returns string in uppercase.
String trim
  • Trims the characters in chars from both ends of string. Chars defaults to whitespace. 
String trimleft
  • Trims the characters in chars from the beginning of string.
String trimright
  • Trims the characters in chars from the end of string. Chars defaults to whitespaces.
Lists
  • A Tcl list is a sequence of values
  • A list has its elements separated by white space
  • Braces or quotes can be used to group words with white space into a single list element
  • Tcl lists are just strings with a special interpretation
  • Example: A List
  • {a b c d e f}
  • Tcl list is string consisting of zero or more space separated words.
Some Examples of lists:
  • set colorList1 {red black orange}
  • set colorList2 [list red white blue]
  • set colorList3 [split "red_grey_blue" _]
List Commands
  • list command is used to create a list
  • list arg1 arg2 arg3 ....
  • The list command constructs a list out of its arguments so that there is one list element for each argument
  • Example: The list command
set x {1 2 3}
set y vlsi 
set list1 [list $x " a b" $y] 
puts $list1
Output: {1 2 3} {a b} vlsi
lindex
  • The lindex command returns the ith element from the list 
  • lindex list l
  • set list1 [a b c d} 
  • puts [lindex $list1 3]
  • Output: d
llength
  • llength returns the number of elements in the list 
  • llength list
  • set list1 [a b c d} 
  • puts [llength $list1]
  • Output: 4
lrange
  • The Irange command returns the ith through jth elements from list 
  • Irange list i j
  • set list { a b c d e} 
  • puts [ Irange $list 1 3]
  • Output: b c d
lappend
  • The lappend appends elements to the value of listvar
  •  lappend listvar arg arg...
  • set list1 {A B} 
  • lappend list1 a b c d
  • puts $list1
  • Output: A B a b c d
linsert
  • linsert inserts elements into list before the element at position index 
  • linsert list index arg arg..
  • set list1 {A B C} 
  • set a [linsert $list1 1 x y z] 
  • puts $a
  • Output: A x y z B C
      lreplace
      • Ireplace replaces elements i through j of list with the args 
      • Ireplace list i j arg arg...
      • set list1 {A B} 
      • set a [Ireplace $list1 1 2 x y z] 
      • puts $a
      • Output: A x y z
          lsearch
          • Isearch returns the index of the element in list that matches the value according to the mode, which can be -exact, -glob or -regexp
          • Isearch ?mode? list value
          • -glob is the default search
          • Returns -1 if not found
          • set list1 {a b c d} 
          • set a [Isearch $list1 d] 
          • puts $a
          • Output: 3
          Concat
          • concat command joins multiple lists together into one list 
          • concat list, list...
          • set a {a b}
          • set y [ concat 0 $a 2] 
          • puts $y
          • Output: 0 a b 2
              join
              • join command merges the elements of a list together by separating them with joinstring
              • join list joinstring
              • set a [join { 1 {2 3} {4 5 6 7 8}} :] 
              • puts $a
              • Output: 1:2 3: 4 5 6 7 8
                split
                • split command splits a string up into list elements using the characters in splitchars as boundaries between list elements
                • split string splitchars
                • set a [split x:y:z:] 
                • puts $a
                • Output:  x y z
                  lreverse
                  • This command is used to reverse the list of string.
                  • % lreverse " x y z "
                  • Output: z y x
                  lshort
                  • This command is used to arrange the list in alphabetical order.
                  • % lsort " c j w b a "
                  • Output: a b c j w
                  Arrays
                  • The array command provides information about the elements currently defined for an array variable
                  • The first argument to the array command is various information like size, elements etc
                  • The final argument to the array command is the name of the array being queried
                  • set ArrayName(Index) value
                  array size
                  • array size returns the number of elements in the array
                  set a(0) 1
                  set a(1) 2
                  set a(2) 3
                  set a(3) 4
                  puts [array size a]
                  Output: 4
                  array names
                  • array names returns the list of the names of the elements in the array
                  set a(0) 1
                  set a(1) 2
                  set a(2) 3
                  set a(3) 4
                  puts [ array names a]
                  Output: 0 1 2 3
                  SHARE

                  vlsi4freshers

                  Hi I’m Designer of this blog.

                    Blogger Comment
                    Facebook Comment

                  0 comments:

                  Post a Comment