3.1 File Input and Output
3.2 Formatting Output
3.3 Procedures
3.4 Masking Special Characters, Substitutions
3.5 Files and Directories
3.6 System Calls
3.7 Handling Errors
3.8 Manipulating Strings
3.9 Assignment
All examples discussed in this section are available in the directory
Applications_Library/GettingStarted/tcl/tcl_basics.
Click to view the primary file
gtclsh_tcl.cmd.
set FIDw [open Writing.tmp "w"]
Here, the file Writing.tmp is opened for writing.
FIDw is a regular Tcl variable, which contains the file
identifier.
puts $FIDw "This is the first line of this file" puts $FIDw $ABCList
close $FIDw
set FIDr [open Writing.tmp "r"]
while { [gets $FIDr NewLine] > 0 } { puts $NewLine } close $FIDr #-> This is the first line of this file #-> a b c d e f
Here, the while loop detects the end of the file.
The gets command returns the number of bytes read. If gets
reaches the end of the file, it returns -1. The while
loop tests for this condition.
set FIDr [open Writing.tmp "r"] set DATA [read $FIDr] puts $DATA close $FIDr #-> This is the first line of this file #-> a b c d e f
Files that contain special Tcl characters often cannot be read line by line. The read command does not have these problems.
Use the format functions to control the formatting of variables during printing:
set pi [ expr 2.0*asin(1.0) ] puts "Pi unformated: $pi" #-> Pi unformated: 3.141592653589793 puts "Pi with 2 digits: [format %.2f $pi]" #-> Pi with 2 digits: 3.14 puts "Pi in exponential format: [format %.4e $pi]" #-> Pi in exponential format: 3.1416e+00 set i 24 puts "Integer with leading zeros: >[format %05d $i]<" #-> Integer with leading zeros: >00024< puts "Integer with leading blanks:>[format %5d $i]<" #-> Integer with leading zeros: > 24<
Define a procedure that computes an Arrhenius law, A = A0 exp(–E/kT), where A0 is the pre-exponential factor and E is the activation energy. The temperature is given by the global variable T.
proc Arrhenius { A0 E } { global T set k 8.62e-5 ; # eV/K set A [expr $A0*exp(-$E/($k*$T))] return $A }
Procedures can be defined anywhere in the Tcl script. However, the procedure can be called only after it was defined.
set T 300 set A [Arrhenius 1e5 1.0] puts "The Arrhenius expression gives: [format %.4e $A]" #-> The Arrhenius expression gives: 1.6067e-12
The dollar sign $, brackets [], and braces {} are special characters in Tcl. If these characters are used in a string, for example, they must be masked, that is, they must be preceded by a backslash (\).
set T 400.0 set CMD_Static "[Arrhenius 1e5 1.0]" puts [format "\[Arrhenius 1e5 1.0\] gives %.4e" $CMD_Static] #-> [Arrhenius 1e5 1.0] gives 2.5378e-08 set T 1100.0 puts [format "\[Arrhenius 1e5 1.0\] gives %.4e" $CMD_Static] #-> [Arrhenius 1e5 1.0] gives 2.5378e-08
Here, the variable CMD_Static contains the value of the Arrhenius expression, evaluated at the time the variable is defined. A later change in the temperature has no effect.
However, masking the function call with \ evaluates the function Arrhenius only when called with the expr command:
set T 400.0 set CMD_Dynamic "\[Arrhenius 1e5 1.0\]" puts $CMD_Dynamic #-> [Arrhenius 1e5 1.0] puts [format %.4e [expr $CMD_Dynamic]] #-> 2.5378e-08 set T 1100.0 puts [format %.4e [expr $CMD_Dynamic]] #-> 2.6291e+00
set FIDw [open TMP_1.tmp "w"] puts $FIDw "test" close $FIDw set FIDw [open TMP_2.tmp "w"] puts $FIDw "test" close $FIDw set FILES [glob "TMP*"] puts "$FILES" #-> TMP_1.tmp TMP_2.tmpHere, two files TMP_1.tmp and TMP_2.tmp are created. Then, a list of all files in the current working directory starting with TMP is created.
set FILE [lindex $FILES 0] set STEM [file rootname $FILE] puts "The rootname is: $STEM" #-> The rootname is: TMP_1 set EXT [file extension $FILE] puts "The extension is: $EXT" #-> The extension is: .tmp
set CWD [pwd] puts $CWD
set PATH [file dirname $CWD] puts "Path is: $PATH" set DIR [file tail $CWD] puts "Directory is: $DIR"
exec rm -f Writing.tmpHere, the UNIX command rm -f Writing.tmp is called from within Tcl.
set FILE source.tcl eval exec rm -f $FILEThe eval command forces the expansion of all Tcl variables and expressions.
set ls_output [exec ls] puts "The output of the ls command is:" puts $ls_output
Tcl terminates the execution of a script if an error occurs. Use the catch command to suppress the termination:
set Nom 0.0 set Denom 10.0 if { [catch { set result [expr $Denom/$Nom]} ErrCode] != 0 } { puts "An Error occured. The Error code is >$ErrCode<" } else { puts "$Denom/$Nom = $result" } #-> An Error occured. The Error code is >divide by zero<
The operation of interest is set result [expr $Denom/$Nom]. If this operation fails, catch suppresses the termination of the script, assigns the error code to the variable ErrCode, and returns a nonzero value.
The if block checks for the nonzero return code and branches accordingly.
As an example, you will work on a string containing information about a simple structure description, consisting of a material name, a region name, and a geometric object. (Note that double quotation marks, braces, and brackets must be masked by a backslash as they have specific meaning in Tcl.)
set STRING "Silicon \"substrate\" \{ rectangle \[(0.0,-0.5) (1.0,0.5)\] \}"
set itmp [expr [string first " " $STRING]] set MATERIAL [string range $STRING 0 [expr $itmp -1]] set istart [expr [string first "\"" $STRING]] set iend [expr [string last "\"" $STRING]] set REGION [string range $STRING [expr $istart+1] [expr $iend -1]] puts "The material is: $MATERIAL" #-> The material is: Silicon puts "The region name is: $REGION" #-> The region name is: substrate
The functions string first and string last return the index of the first or last occurrence of a given pattern, here whitespace or a double quotation mark, respectively. The function string range returns a substring, which starts and ends at the indices given.
The coordinates X0, Y0, X1, and Y1 can be extracted in a similar fashion:
set istart [expr [string first "(" $STRING]] set iend [expr [string first "," $STRING]] set X0 [string range $STRING [expr $istart+1] [expr $iend -1]] set istart [expr [string first "," $STRING]] set iend [expr [string first ")" $STRING]] set Y0 [string range $STRING [expr $istart+1] [expr $iend -1]] set istart [expr [string last "(" $STRING]] set iend [expr [string last "," $STRING]] set X1 [string range $STRING [expr $istart+1] [expr $iend -1]] set istart [expr [string last "," $STRING]] set iend [expr [string last ")" $STRING]] set Y1 [string range $STRING [expr $istart+1] [expr $iend -1]]
set NewString ${MATERIAL}_${REGION} puts $NewString #-> Silicon_substrate
set Text "$NewString extends from " append Text "X= $X0 to $X1 " append Text "and from " append Text "Y= $Y0 to $Y1." puts $Text #-> Silicon_substrate extends from X= 0.0 to 1.0 and from Y= -0.5 to 0.5.
set NAME1 "Eva" set NAME2 "Adam" if { [string compare $NAME1 $NAME2] == 0 } { puts "Both names are the same" } elseif { [string compare $NAME1 $NAME2] < 0 } { puts "$NAME1 comes before $NAME2" } elseif { [string compare $NAME1 $NAME2] > 0 } { puts "$NAME2 comes before $NAME1" } #-> Adam comes before Eva
set DEV "HV NMOStransistor" if { [string match "*NMOS*" $DEV] } { puts "This device is an NMOS" } else { puts "This device is NOT an NMOS" } #-> This device is an NMOS
set OLDString "HV NMOStransistor" set OLDPattern "NMOS" set NEWPattern "PFET" set NEWString [regsub $OLDPattern $OLDString $NEWPattern] puts "NEW String: $NEWString" #-> NEW String: HV PFETtransistor
set String "1 2 3 4" set SUM 0 foreach Number $String { set SUM [expr $SUM+$Number] } puts $SUM #-> 10
Use the split command if the entries are separated by a different symbol, such as a comma-separated value (CSV):
set String "1,2,3,4" set List [split $String ","] set SUM 0 foreach Number $List { set SUM [expr $SUM+$Number] } puts $SUM #-> 10
The CSV file vtrolloff.txt included in the tcl_basics project contains a set of experimental results. Each line has the entries gate length in nm (Lg), drain bias in V (Vd), threshold voltage in V (Vti), as well as some other values.
Click to view the CSV file vtrolloff.txt.
For each gate length, the threshold voltage is given for the linear regime (VtLin, Vd = 0.05 V) and for the saturation regime (VtSat, Vd = 1.25 V). The experiments are not sorted with respect to the gate length.
Write a Tcl script that reads the CSV file and computes, for each gate length, the drain-induced barrier lowering (DIBL) voltage (DIBL = VtLin – VtSat).
Click to view a solution in the primary file gtclsh_tcl.cmd. Search for ### 3.9.
Copyright © 2022 Synopsys, Inc. All rights reserved.