ChemShell logo

ChemShell Tutorial

Search the manual:

Very basic Tcl for ChemShell

This part of the ChemShell tutorial assumes you are running an interactive shell, started by issuing the command chemsh.

Start with the command puts which outputs a string

% puts "hello world"
hello world
%

Like all programming languages, Tcl can store data in variables, which can be thought of as labelled containers for information. Values can be assigned to variables using the command set. Variables can contain numbers, text, and many other types of information. The value of a variable can be accessed by prefixing it with a dollar sign.

% set a 1
1
% puts "The value of a is $a"
The value of a is 1
% set b "word"
word
% puts "Hello $b"
Hello word

The dollar sign can be used to set one variable to the value of another.

% set a 3
3
% set b $a
3
% puts "The value of a is $a and the value of b is $b"
The value of a is 3 and the value of b is 3
%

All variables in Tcl, including numbers, are treated as strings (lists of characters). An important feature of Tcl is the way compound strings, known as lists, are built up from primitive strings, or indeed from other lists.

The list structure is generated automatically from a white-space separated list, contained by " " or { }.

% set v "a b c"
a b c
% puts "List v contains: $v"
List v contains: a b c
%

The values of other variables can be included in lists using the dollar sign. Note that this only applies to lists contained in quotes. If you do not want a variable reference converted into a value, use curly brackets.

% set a 1
1
% set b 2
2
% set list1 "$a $b $a"
1 2 1
% set list2 {$a $b $a}
$a $b $a
%

Lists can be manipulated by a family of commands (see the list manual page). For example, the llength command returns the number of items in the list, while lindex returns a specific item identified by its position (counting from 0). Note that they operate on the value of the variable ($v).

% set v "a b c"
a b c
% llength $v
3
% lindex $v 2
c
%

The results of commands can be included in other commands using square brackets:

% puts "There are [ llength $v ] items in v"
There are 3 items in v
% puts "v in reverse order is [ lindex $v 2] [ lindex $v 1] [ lindex $v 0] "
v in reverse order is c b a
%

Arithmetic operations are performed through the expr command.

% set a 3
3
% set b 2
2
% set c [ expr $a + $b ]
5
% puts "The value of c is $c"
The value of c is 5
%

Neat output can be produced by using the format command.

% puts [format "%e + %e = %e" $a $b $c]
3.000000e+00 + 2.000000e+00 = 5.000000e+00
% puts [format "%f + %f = %f" $a $b $c]
3.000000 + 2.000000 = 5.000000
% puts [format "%d + %d = %d" $a $b $c]
3 + 2 = 5
% puts [format "%s + %s = %s" $a $b $c]
3 + 2 = 5
%

The format identifiers are %s for strings, %d for integers, and %f and %e for floating-point and scientific numbers, respectively. The length of the fields can be specified as well.

Commands may be nested using the [ ] notation:

% puts [format "%s + %s = %s" $a $b [expr $a + $b] ]
3 + 2 = 5
%

More information is available in the respective part of the ChemShell manual, or the official Tcl tutorial. The Tcl manual provides a reference for all built-in Tcl commands.

Back to main ChemShell tutorial