next up previous contents
Next: Advanced variables: compound expressions Up: Under the hood Previous: Policing primitives   Contents

Advanced variables: structures

When using ``structures'', a single variable can also contain multiple values. This concept is very similar to C.

A structure contains so-called ``entries''. Each entry has a name, and can contain any value that may be stored in a variable. Structure entries are created or changes by assigning to a variable which is followed by a dot (.) and the entry name. Likewise, structure entries are read by appending a dot and the entry name to the variable name.

Example:

$a.x = 6;
$a.y = 7
$a.z = $a.x*$a.y;

The value of a structure entry may also be another structure, whose entries are then accessed by adding more dots and entry names.

Example:

$a = 1;
$a.b = 2;
$a.b.c = 4;
$a.b.c.d = $a+$a.b+$a.b.c;

Note that assigning a structure copies all fields, and that overwriting a structure value removes the whole structure.

Examples:

$a.x = 1;
$a.y = "foo";
$b = $a;  // now $b.x is 1, and $b.y is "foo"
$a = 13;  // this also eliminates $a.x and $a.y

Also note that structures are treated like single variable values, so scoping applies to the whole structure, but not to individual structure entries.

Example:

$x.y = 13;
{
    $x.y = 7;
}

At the end, $x.y has the value 7, not 13.

Note that structure entries cannot be forward-referenced, because they are part of the value of the corresponding variable, and not variables by themselves.

Also note that in tcng, ``.'' is treated as a part of the variable name, and not as an operator. Therefore, constructs like ($var).flt are not allowed, and yield a syntax error. This restriction may be lifted in the future.


next up previous contents
Next: Advanced variables: compound expressions Up: Under the hood Previous: Policing primitives   Contents
Martin A. Brown 2003-11-06