A brief synopsis of Tcl/Tk syntax

The user of Tcl/Tk types in commands after the prompt sign ``%.'' The result of each command is displayed on the console window. A command is invoked by typing
funcname arg1 ... argn
In other words a command is separated from its arguments by spaces, and the arguments are separated from each other by spaces. The end of the argument list is signaled by the end of the line. To continue a command onto a new line, use a backslash as the last character of the line.

Here is an example of a Tcl/Tk command:

set a 4
This command creates a variable a and sets it to 4. Variables in Tcl/Tk can hold only strings, so the fact that 4 is an integer does not have any significance to the set command. The name of the variable (in this case "a") in Tcl can be fairly arbitrary strings, but it is suggested that you stick with letters, digits, and underscores.

If an argument itself has a space character in it, then you have to enclose the argument in quotation marks or curly braces:

set a "p 4"
set a {p 4}
Note that, unlike other languages, quotation marks do not differentiate string-literals from non-literals. The two commands set b a and set b "a" have identical meaning in Tcl/Tk.

To access the value of a variable, you must precede the name with a dollar-sign. Thus, after the commands

set a 4
set b $a
both a and b are assigned the value 4. The set command has the property that in addition to setting the variable to the value, it also returns the value (and in console mode, displays it on your console). Because the QMG functions typically return enormous strings, QMG defines a variant of "set" called
gmset which suppresses the display of the answer on your console.

Another example of a command is the expr command that causes a string to be evaluated as an arithmetic expression, and the value of that expression is returned. For example if you typed

set a 4
expr "$a + 19"
then the second line would return 23.

A second syntactic feature of Tcl/Tk is square-brackets. Square brackets invoke a function. Thus,

set a 4
set b [expr $a+19]
will set b to 23. A final major syntactic feature is curly-braces. Curly braces have several properties: like quotes, they suppress spaces as separators. Unlike quotes, they can be nested. In addition, unlike quotes, they also prevent square brackets from being evaluated and dollar-sign substitution. You need curly braces to denote matrices and vectors to QMG. Here is an example. Suppose you want to set t to be a regular triangle, and t1 to be a skewed version of that triangle with the matrix [1,1,0;0,1,0] applied to it (this transformation applies a skew but no translation). The command in QMG to define a regular polygon is gmpolygon and to apply a transformation gmapply. Thus, you would type:
gmset t [gmpolygon 3]
gmset t1 [gmapply {{1 1 0} {0 1 0}} $t]
Unforunately, the following will not work:
gmset t [gmpolygon 3]
set skewamount 1
gmset t1 [gmapply {{1 $skewamount 0} {0 1 0}} $t]
The reason this fails is that (as mentioned above) curly braces prevent dollar-sign substitution, so this statement would cause the string literal $skewamount to be passed to the gmapply routine, but gmapply is expecting numbers. How do you get around this problem? You need to use the list instruction in Tcl:
gmset t [gmpolygon 3]
set skewamount 1
gmset t1 [gmapply [list [list 1 $skewamount 0] [list 0 1 0]] $t]
At this point you probably should get a book to learn more! There is also information on the web.

Tcl include procedure and looping constructs to build arbitrarily complex programs. In addition, Tk makes it easy to attach a graphical user interface to Tcl scripts.

This documentation is written by Stephen A. Vavasis and is copyright (c) 1996 by Cornell University. Permission to reproduce this documentation is granted provided this notice remains attached. There is no warranty of any kind on this software or its documentation. See the accompanying file 'Copyright' for a full statement of the copyright.

Back to the QMG1.1 home page.

Stephen A. Vavasis, Computer Science Department, Cornell University, Ithaca, NY 14853, vavasis@cs.cornell.edu