Tool Command Language
3. Other Useful Tcl Commands

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

Objectives

All examples discussed in this section are part of the GettingStarted project, which can be found in the directory Applications_Library/GettingStarted/tcl/tcl_basics.

Click to view the master file gtclsh_tcl.cmd.

3.1 File Input and Output

Files that contain special Tcl characters often cannot be read line by line. The read command does not have these problems.

3.2 Formatting Output

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<

3.3 Procedures

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

3.4 Masking Special Characters, Substitutions

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

3.5 Files and Directories

3.6 System Calls

3.7 Handling Errors

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.

3.8 Manipulating Strings

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)\] \}"

3.9 Assignment

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 master file gtclsh_tcl.cmd. Search for ### 3.9.

main menu    |   module menu    |   << previous section    |   next section >>