-->

Previous | Table of Contents | Next

Page 503

Internally, however, tcl treats all variables as strings. When a variable needs to be manipulated, tcl allows numbers (real and integer) to be given in all the forms that are understood by ANSI C. The following are examples of valid numeric values for variables:


74 Integer 0112 Octal, starts with a 0 0x4a Hexadecimal, starts with 0x 74. Real 74.0 Real 7.4e1 Real 7.4e+1 Real

Other values are treated as strings and will generate errors if used in mathematical expressions.

Variables

tcl defines two types of variables, scalars and arrays. To create a scalar variable and assign it a value, use the set command. For example,


set banana 1;

creates the variable banana and gives it a value of 1. To set the value of banana to something different, simply use set again:


set banana "Fresh from Brazil";

Now the variable banana has the value "Fresh from Brazil". The double quotes tell tcl that all the characters including the spaces make up the value of the variable. (Quoting and substitution are covered later in this chapter in the section "Quoting and Substitution.")

To print out the value of banana, use the puts command:


puts $banana;

This prints the value of the variable banana to the standard output (sometimes referred to as STDOUT). Putting $ before the name of the variable tells tcl to access the value assigned to that variable. This convention, known as variable substitution, is similar to conventions used in UNIX shells.

To created a one-dimensional array, enter the following:


set fruit(0) banana;

set fruit(1) orange;

This creates the array variable fruit and assigns to the two named items, 0 and 1, the values banana and orange. The assignments to array indexes need not be in order. The commands


set fruit(100) peach;

set fruit(2) kiwi;

set fruit(87) pear;

Page 504

create only three items in the array fruit. This is because arrays in tcl are like associative arrays, which associate a "key" with a value. Arrays in tcl associate a given string with another string. This makes it possible to have array indexes that are not numbers:


set fruit(banana) 100

This sets the value of item banana in the array fruit to 100. The assigned values need not be numeric:


set food(koala) eucalyptus;

set food(chipmunk) acorn;

To access the value stored in a one-dimensional array variable, use the $ convention:


puts $food(koala);

This prints out the value stored in the array food at index koala. The array index can also be a variable:


set animal chipmunk;

puts $food($animal);

These commands will output acorn, given the previous assignments.

Multidimensional arrays are a simple extension of one-dimensional arrays. They are set as
follows:


set myarray(1,1) 0;

This sets the value of the item at 1,1 in the array myarray to be 0. By separating the indexes by commas, you can make arrays of three, four, or more dimensions:


set array(1,1,1,1,1,1) "foo";

In addition to setting array values, tcl provides the array command for getting information about arrays and the parray command for printing out information about arrays. First, take a look at the parray command. Given the declarations


set food(koala) eucalyptus;

set food(chipmunk) acorn;

set food(panda) bamboo;

the command


parray food

will produce the following output:


food(chipmunk) = acorn

food(koala)  = eucalyptus

food(panda)  = bamboo

Now look at the array command and its arguments, which are used to get information about an array and its elements. The basic syntax for an array command is as follows:


array option arrayname

Page 505

The supported options are discussed later in this section.

One of the most frequently used pieces of information about an array is its size. Given the declarations


set fruit(0) banana;

set fruit(1) peach;

set fruit(2) pear;

set fruit(3) apple;

The command


array size fruit;

will return 4. This number is often useful in loops.

Because arrays can have nonsequential or nonnumeric indexes, the array command provides an option for getting elements from an array. Assuming that the food array has been defined as presented earlier, the first thing you need to do to start getting elements is to use startsearch through the array. This is accomplished by first getting a search ID for the array:


set food_sid [array startsearch food];

The command


array startsearch food

returns a string, which is the name of the search (see the section called "Quoting and Substitution"). You will need this for future reference, so set its value to that of a variable, in this case food_sid.

To get the first element (and every subsequent element) of the food array, use the following:


array nextelement food $food_sid;

When the array search is done, terminate the search for the array using


array donesearch food $food_sid;

One other option to the array command that is frequently in use while iterating through an array is the anymore option. It returns true (a value of 1) if there are any more items in the search. For example,


array anymore food $food_sid;

returns 1 the first two times it is used with the food array declared earlier.

To dispose of a variable (scalar or array), use the unset command:


unset banana;

This unsets the variable banana. If you used unset $banana (assuming that banana was set to the value shown earlier) instead of just banana, you would get an error like:


can't unset "0": no such variable

Previous | Table of Contents | Next