2.1 Simple Variables
2.2 Arithmetic Expressions
2.3 Strings
2.4 Lists
2.5 Loops
2.6 Arrays
2.7 Conditional Branching
2.8 Logical Operators
2.9 Assignment
All examples discussed in this module are available in the directory
Applications_Library/GettingStarted/tcl/tcl_basics.
Click to view the primary file
gtclsh_tcl.cmd.
In Tcl, variables do not need to be declared before being used and they do not have a type. This means that the difference between an integer, a floating-point variable, and a string is only how the variable is used:
set i 3 set q 1.6e-19 set W "Hello World"
The dollar sign ($) is used to access the value of a variable. (The puts command writes to standard output, that is, to the screen.)
puts "The value of i is $i" #-> The value of i is 3 puts "The elementary charge is $q C" #-> The elementary charge is 1.6e-19 C puts "The string W contains >$W<" #-> The string W contains >Hello World<
In some cases, it is unclear where the variable name ends and a string starts. In this case, use {} to indicate where the variable name ends:
puts "The ${i}rd value" #-> The 3rd value
Use the Tcl function expr to execute an arithmetic expression:
set j [expr $i+5] puts "$i + 5 is $j" #-> 3 + 5 is 8 set pi [expr 2.0*asin(1.0)] puts "pi = $pi" #-> pi = 3.141592653589793 set SIN [expr sin($pi/4.0)] puts "sin(pi/4.0) is $SIN" #-> sin(pi/4.0) is 0.7071067811865475
Like other programming languages, note the different ways of handling integer and floating-point operations:
puts [expr 1/2] #-> 0 puts [expr 1/2.0] #-> 0.5
Therefore, in most cases, you should append a dot (.) to any integer when doing arithmetics.
In a Tcl script, every value is a string. Every component of each command, from the name that is used to locate its routine, to the arguments that are passed to that routine, is also a string.
If you want to represent multiple words within a string, you should use either double quotation marks or braces:
set MyString "Hello World" set MyVariable {This is the string}
Double quotation marks are not needed for a single word:
set SingleWordString Hello
To combine several strings, use the append command:
append SingleWordString "!" #-> Hello!
To access the string content, use the dollar sign ($) as usual:
set MyString "Hello World" puts $MyString #->Hello World
Several functions allow you to manipulate strings, including the following:
string length $MyString #-> 11
string index $MyString 6 #-> W
string range $MyString 3 7 #-> lo Wo
string equal -nocase "hello" "HeLlO" #-> 1
string match "*ello*" $MyString #-> 1 string match "Hell" $MyString #-> 0 string match "hell*" $MyString #-> 0 string match -nocase "hell*" $MyString #-> 1
set StringToTrim " abcba " string trim $StringToTrim #-> abcba string trim $StringToTrim " a" #-> bcb string trimleft $StringToTrim " a" #-> bcba string trimright $StringToTrim "a " #-> abcb
set string2 "tutorial" string first "o" $string2 #-> 3 string last "t" $string2 #-> 2
string tolower $MyString #-> hello world string toupper $MyString #-> HELLO WORLD
A very basic type of Tcl is a list. A list consists of elements that are separated by whitespace. The first element of a list is associated with the index 0.
set ABCList [list a b c d e] set NUMList [list 1 2 3 4 5 6] set STRList [list This sentence is a TCL list] set MIXList [list a 2 3.1415 TCL ?] set EMPTYList [list]
puts $ABCList #-> a b c d e puts $NUMList #-> 1 2 3 4 5 6 puts $STRList #-> This sentence is a TCL list puts $MIXList #-> a 2 3.1415 TCL ?
set LENGTH [llength $ABCList] puts "ABCList contains $LENGTH elements." #-> ABCList contains 5 elements
puts "The first element of ABCList is: [lindex $ABCList 0]" #-> The first element of ABCList is: a puts "The second element of NUMList is: [lindex $NUMList 1]" #-> The second element of NUMList is: 2 puts "The last element of MIXList is: [lindex $MIXList end]" #-> The last element of MIXList is: ? puts "The next to last element of ABCList is \ [lindex $ABCList [expr $LENGTH -2]]" #-> The next to last element of ABCList is d set SUBList [lrange $STRList 1 3] puts "The second to fourth elements of STRList are >$SUBList<" #-> The second to fourth elements of STRList are >sentence is a<
set cIndex [lsearch $ABCList "c"] puts "The letter c has the index $cIndex" #-> The letter c has the index 2
set NewElement f lappend ABCList $NewElement
set UnsortedList [list 45.1 78.6 12.6 1.5 89.4 11.6] set SortedList [lsort -real $UnsortedList] puts $SortedList #-> 1.5 11.6 12.6 45.1 78.6 89.4
The most commonly used lsort flags are:
set UnsortedList [list {a 45.1 1} {g 78.6 5} {r 12.6 8} \ {c 1.5 2} {q 89.4 3} {n 11.6 4}] set SortedList_0 [lsort -index 0 -ascii $UnsortedList] puts $SortedList_0 #-> {a 45.1 1} {c 1.5 2} {g 78.6 5} {n 11.6 4} {q 89.4 3} {r 12.6 8} set SortedList_1 [lsort -index 1 -real $UnsortedList] puts $SortedList_1 #-> {c 1.5 2} {n 11.6 4} {r 12.6 8} {a 45.1 1} {g 78.6 5} {q 89.4 3} set SortedList_2 [lsort -index 2 -integer $UnsortedList] puts $SortedList_2 #-> {a 45.1 1} {c 1.5 2} {q 89.4 3} {n 11.6 4} {g 78.6 5} {r 12.6 8}
This section describes different loop operations that can be performed using Tcl.
The foreach loop operates on lists:
foreach NUM $NUMList CHAR $ABCList { puts "The ${NUM} letter of the alphabet is $CHAR" } #-> The 1 letter of the alphabet is a #-> The 2 letter of the alphabet is b #-> The 3 letter of the alphabet is c #-> The 4 letter of the alphabet is d #-> The 5 letter of the alphabet is e #-> The 6 letter of the alphabet is f
A foreach loop steps through the given lists, here NUMList and ABCList, and executes the body, here a simple puts for each element of the list.
The current elements of the list are stored in the variables NUM and CHAR. All lists given to the foreach loop should have the same number of elements. Otherwise, they are padded with empty elements {}.
The for loop uses a counter that is incremented in each pass. In the following for loop, the variable i takes the values 0, 1, 2, ..., 10:
for { set i 0 } { $i <= 10 } { incr i } { puts -nonewline " i=$i " } #-> i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9 i=10
The first argument of the for loop initializes the counter, here i is set to zero. The second argument defines the end condition, here, the loop is executed until the variable i is no longer less than or equal to 10. The third argument defines the increment of the counter after each pass, here, the counter is incremented by 1.
The spaces at the locations marked by * are required:
for*{ set i 0 }*{ $i <= 10 }*{ incr i }*{...}
The while loop offers more flexibility than the for loop. The end condition can be anything.
set f 1.0 set x 0.0 while { $f > 0.0 } { set x [expr $x + 0.01] set f [expr 1.0 - $x*$x] } puts "Zero crossing is at x= $x" #-> Zero crossing is at x= 1.0
Here, a while loop is used to find the zero crossing of the function f = 1 – x2. In each pass, x increases, and the function f is evaluated. The process stops when f is no longer positive.
The loop flow can be influenced by using the following keywords:
for {set i 0} {$i<=100} {incr i} { if {[expr fmod($i, 2)] == 0} { continue } elseif {$i > 6} { break } puts $i } #-> 1 #-> 3 #-> 5
An array contains elements that can be addressed using an identifier.
set model(1) Fermi set model(2) Constant set model(3) 3Stream set model(4) 5Stream for { set i 1 } { $i <= 4 } { incr i } { puts "Model #$i is $model($i)" } #-> Model #1 is Fermi #-> Model #2 is Constant #-> Model #3 is 3Stream #-> Model #4 is 5Stream
Here, the identifier is an integer index. However, in Tcl, the array identifier does not have to be an integer or a number:
set Identifiers [list first second third fourth] set model(first) Fermi set model(second) Constant set model(third) 3Stream set model(fourth) 5Stream foreach i $Identifiers { puts "The $i model is $model($i)" } #-> The first model is Fermi #-> The second model is Constant #-> The third model is 3Stream #-> The fourth model is 5Stream
If a variable is used as an array, it can no longer be used as a normal variable unless the array is deleted using array unset <variable name>.
The if blocks execute conditional code:
set val 1 if { $val == 0 } { puts "val is 0" } elseif { $val > 2 } { puts "val is larger than 2" } else { puts "val is negative or one" } #-> val is negative or one
The additional elseif test condition and the default else are optional.
The main comparators are:
The switch blocks are more compact than the if blocks, but they are restricted to tests for equality:
set MODEL Fermi switch $MODEL { Constant { puts "Use constant diffusion model" } Fermi { puts "Use Fermi diffusion model"} } #-> Use Fermi diffusion model
Use logical operators to create more complex conditions:
set Vd 1.5 set Device "pMOS" set Simulation "IV" if { $Vd < 0.1 && $Device == "nMOS" && $Simulation != "BV" } { puts "Simulate nMOS IdVg in linear regime" } elseif { $Vd > 0.1 && $Device == "pMOS" && ! ($Simulation == "BV") } { set Vd [expr -1.0*$Vd] puts "Simulate pMOS IdVg for Vd=$Vd" } elseif { $Simulation == "BV" || $Device == "BJT" } { puts "Simulate BJT or MOS breakdown" } else { puts "None of the specified conditions are met..." } #-> Simulate pMOS IdVg for Vd=-1.5
Using the NOT operator ! directly before () will confuse the Sentaurus Workbench preprocessor, as it is misinterpreted as the start of a Tcl preprocessing block. To avoid this, insert a space between the exclamation mark and the opening parenthesis: ! ().
The following Tcl lists contain a vector of frequency points and corresponding current gain values (h21). In this assignment:
Use the following data:
set freqList [list 1.00e5 2.15e5 4.64e5 1.00e6 2.15e6 4.64e6 \ 1.00e7 2.15e7 4.64e7 1.00e8 2.15e8 4.64e8 \ 1.00e9 2.15e9 4.64e9 1.00e10 2.15e10 4.64e10 \ 1.00e11 2.15e11 4.64e11 1e+12 ] set h21List [list 33.62 33.62 33.62 33.62 33.62 33.62 \ 33.62 33.61 33.59 33.46 32.86 30.48 \ 23.74 14.12 7.06 3.34 1.57 0.75 \ 0.38 0.19 0.20 0.23 ]
Click to view a solution in the primary file gtclsh_tcl.cmd. Search for ### 2.9.
Copyright © 2022 Synopsys, Inc. All rights reserved.