next up previous
Next: Expressions Up: An introduction to Previous: Commands

Variables

The command in the previous example set the variable myvar to the string "27". The only data type in Tcl is a string. However, some commands will interpret arguments as numbers, for example the expr command (expr will be explained later).

To use the value of a variable, you prepend it with :

% puts myvar
27
(puts is the command to print some value.)

String values are normally delimited with double quotes, but a string of a single word doesn't need them:

% set myvar "hello world!"
hello world!
% puts myvar
hello world!
% set myvar hello
hello
% puts myvar
hello

The value of a variable can be used when defining a string by prepending it with a :

% set myvar "world!"
world!
% set myvar2 "hello myvar"
hello world!
% puts myvar2
hello world!

To include a literal '' in a string, use $:

% set myvar "hello$world!"
helloworld!

Variables are not declared, but come into existence as soon as they get a value:

% puts unknownvar
can't read "unknownvar": no such variable
% set unknownvar "known"
known
% puts unknownvar
known
A variable can be destroyed by unsetting it:
% puts unknownvar
known
% unset unknownvar
% puts unknownvar
can't read "unknownvar": no such variable



SE Praktikum
Tue Aug 13 13:33:30 MET DST 1996