Getting Started Documentation Glish Learn More Programming Contact Us
Version 1.9 Build 1556
News FAQ
Search Home


next up previous contents index
Next: Attributes Up: Values, Types, and Constants Previous: Strings

Subsections



Records

A record is a collection of values. Each value has a name, and is referred to as one of the record's fields. The values do not need to have the same type, and there is no restriction on the allowed types (i.e., each field can be any type).


Record Constants

You create record constants in a manner similar to vector constants, by enclosing values within square brackets ([]). Unlike vectors, though, each value must be preceded with a name and an equal sign (=). For example:

    r := [foo=1, bar=[3.0, 5.3, 7], bletch="hello there"]
creates a record r with three fields, named ``foo", ``bar", and ``bletch". These fields have types integer, double, and string, respectively. Empty records can be created using [=]:
    empty := [=]
As explained in § 3.1.4, page [*], if [] is used instead of [=] then empty will have type boolean instead of record.


Accessing Fields Using ``.''

You access record fields using the ``.'' (dot or period) operator, as in many programming languages. Continuing the above example for the record r,

    r.bar
denotes the three-element double vector
    [3.0, 5.3, 7]
and
    r.bar[2]
is the double value 5.3. Field names specified with ``.'' must follow the same syntax as that for Glish variable names (see § 4.1, page [*]), namely they must begin with a letter or an underscore (``_'') followed by zero or more letters, underscores, or digits. Unlike variable names, Glish reserved words such as if or whenever are legal for field names. Field names are case-sensitive.

You can assign to a record field using the ``.'' operator, too. After executing

    r.date := "30Jan92"
r now has four fields, the fourth named date.

The length (or len) function returns the number of fields in a record. For our running example,

    len(r)
now returns the integer value 4.

The field_names function returns a string vector whose elements are the names of the fields of its argument, in the order the fields were created. For example, at this point

    field_names(r)
yields the vector
    ["foo", "bar", "bletch", "date"]


Accessing Fields Using []

In addition to using the ``.'' operator to access fields, records can also be indexed using []'s with string-valued indices. For example,

    r["bar"]
is equivalent to r.bar. Furthermore, the index does not need to be a constant; any string-valued expression will do:
    b := "I'll meet you at the bar"
    print r[b[6]]
prints r's bar field.

Just as the ``." operator can be used to assign record fields, so can []:

    r["date"] := "30Jan92"
is equivalent to the example using r.date above.

When accessing fields using [], any string can be used, not just those conforming to the field names allowed with the ``." operator (see above). For example,

    expletive['&)#% (&%!'] := T
is legal. Field names with embedded asterisks (``*''), though, are reserved for internal use by Glish.

There are also mechanisms for accessing or modifying more than one field at a time. (See § 3.6, page [*].)


Accessing Fields Using Numeric Subscripts

You can also index records using [] with numeric subscripts, much as with vectors. For example,

    r[3]
refers to the third field assigned to r; for our running example this is bletch, a 2-element string vector. As with vectors, all indexing operations are checked to make sure the index is within bounds (between 1 and the length of the record).

You can then alter record fields by assigning to them in the same fashion:

    r[3] := F
changes the bletch field to be a scalar boolean value. New fields are created by assigning to r using an index one greater than the number of fields in r. For our running example r has 4 elements, so
    r[5] := [real=0.5, imag=2.0]
adds a new field to r with a value that has a record with two fields. The field is given an arbitrary, internal name, guaranteed not to conflict with other fields in r and containing an embedded `*' character.

It is not legal to add a new field to a record at a position greater than one more than the number of fields. For example,

    r[7] := [1, 4, 7]
is illegal since len(r) is 5.

An important point is that vector-style indexing of records allows the creation of ``arrays" with elements that have different types. For example,

    a := [=]
    a[1] := 32
    a[2] := "hello there"
    a[3] := [field1=T, field2="the more the merrier"]
creates what is for most purposes (see § 3.6.4, page [*], for exceptions) an ``array" a with the first element an integer, the second a string, and the third a record. While a number of Glish types (record, function, agent, and reference) are not ``vector types'' in the sense that each value of the type is implicitly a vector, arrays of these types can be created using records in the above fashion.


next up previous contents index
Next: Attributes Up: Values, Types, and Constants Previous: Strings   Contents   Index
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc., Washington, D.C.

Return to AIPS++ Home Page
2006-10-15