Introduction To Tcl - OpenSeesWiki

Introduction To Tcl

From OpenSeesWiki

Jump to: navigation, search






Contents

Commands

Tcl is a string-based command language with relatively little syntax. Tcl scripts are made up of commands separated by new lines or semicolon (;). The basic syntax for a Tcl command is:

command arg1 arg2 arg3 ...


command is name of the command
$arg1 $arg2 $arg3 ... are the arguments for the command


The command is either the name of a built-in command or a Tcl procedure defined previously by the user. White space (i.e., space or tab) is used to separate the command name and its arguments, and a newline or semicolon is used to terminate a command. The arguments to a command are just strings. Here is an example:

puts "Hello World"

The puts is the command. In this example it takes one argument. In this form puts will take the single string argument at write it to the terminal. The quotation marks " are used to group the words into a single argument, { } braces could also have been used. Tcl has syntax for grouping, which allows multiple words in one argument, and substitution, which is used with programming variables and nested command calls. The Tcl interpreter does grouping first, then substitutions, and finally it calls the command. It is up to the command to interpret its arguments.

Source Command

The interpreters allow you to enter commands one-by-one interactivily at the OpenSees > prompt. An important command for OpenSees users is the source command. The source command reads Tcl commands from a file and evaluates them just as if you had typed them interactively. For example, if the file hellloWorld.tcl contained the example puts command used above, the user could cause the interpreter to execute this command by invoking the following:

source "helloWorld.tcl"

While not very useful for this example, for users creating finite element models and analyzing them it is obviously very useful.

Variables

Another basic command in tcl is the set command. It is used to assign a value to a variable.

set msg "Hello World"

In this example we are setting the variable msg to be equal to the string Hello World'. It is useful to note that a number of predefined variables exist when you start an interpreter. For those programmers who are reading this, the variables, argc , argv , and env (amongst others) can be used in the script.

Command Substitution

Commands in tcl can be nested. A nested command is delimited by square brackets, [ ]. The Tcl interpreter takes everything between the brackets and evaluates it as a command. It rewrites the outer command by replacing the square brackets and everything between them with the result of the nested command.

set msgLength [string length " Hello World"]

Mathematical Expressions

The Tcl interpreter itself does not evaluate math expressions. Instead, the expr command is used to evaluate math expressions. The interpreter treats expr just like any other command, and it leaves the expression parsing up to the expr implementation. The math syntax supported by expr is the same as the C expression syntax. The expr command deals with integer, floating point, and boolean values. Logical operations return either 0 (false) or 1 (true). The typical arithmetic operators, +, -, *, /, %, <, <=, >, >=, ==, !=, ... and the typical mathematical functions sin(), cos(), tan(), sqrt(), log(), log10(), pow(), rand(), srand(), exp(), ... found in many programming languages are available for use in expressions.

set a [expr 3 /2.0]
=> 1.5
set pi [expr 2*asin(1.0)]
=> 3.14159..


A word or warning, integer values are promoted to floating point values as needed, WHICH MEANS THE USER MUST BE WARY OF INTEGER DIVISION.

set a [expr 3 /2.0]
=> 1.5
set b [expr 3/2]
=> 1
set c [expr 3.0/2]
=> 1.5

Procedures

Tcl uses the proc command to define procedures. Once defined, a Tcl procedure is used just like any of the built-in Tcl commands. The basic syntax to define a procedure is:

proc $name $argsList $body

The first argument, $name , is the name of the procedure being defined. The second argument, $argsList , is a list of parameters to the procedure. The third argument, $body, is a command body that is one or more Tcl commands. Here is a simple example that will define a procedure named sum , that will take 2 args and return the sum of these args. That is followed by an example setting the value of the variable c to be result of invoking the procedure sum with the values 3 and 5 .

proc sum {a b} {
  return [expr $a + $b]
}
set c [sum 3 5]
=> 8

FILE I/O

Like any programming language, Tcl allows files to be opened for both reading and writing. The following is an example that will open the file tmp.out and write the factorial for the vaues 1 through 10 to it:

proc factorial {n} {
  set a 1; set product $n;
  while {$a < $n} {
    set product [expr $product * $a];
    set a [expr $a + 1]; # or incr a 1
  }
  return $product
}
 
set file1 [open tmp.out w]
 
for {set i 1} {$i <= 10} {incr i 1} {
   puts $file1 "factorial $i = [factorial $i]"
}
 
close $file1

Tcl Books

  1. Tcl and the Tk Toolkit by John Ousterhout, Addison-Wesley, 1994, ISBN 0-201-63337-X - the seminal book on Tcl and Tk.
  2. Practical Programming with Tcl and Tk by Brent Welch, Prentice Hall, 1995, ISBN 0-13-182007-9.
  3. Graphical Applications with Tcl and Tk by Eric F. Johnson, M&T Books, 1996, ISBN 1-55851-471-6 - Covers cross-platform development with Tcl and Tk on Windows and UNIX.

There are too many books to list all of them here.

Personal tools