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

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 gif, if [] were used instead of [=] then empty would 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 our example for the record r above,

    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 gif), 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 will now have four fields, the fourth being named date.

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

    len(r)
will now return 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 in which the fields were created. For example, at this point

    field_names(r)
would yield 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]]
will print 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 gif, for a description.

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 a record field by assigning to them in the same fashion:

    r[3] := F
changes the bletch field to be a scalar boolean value. New    fields can be created by assigning to r using an index one greater than the number of fields in r. For our running example r has at this point 4 elements, so
    r[5] := [real=0.5, imag=2.0]
adds a new field to r whose value is itself 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]
would be illegal since len(r) is 5.

An important point is that vector-style indexing of records allows   the creation of ``arrays" whose elements 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 gif, for exceptions) an ``array" a whose first element is an integer, second a string, and 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: ValuesTypes, and Constants Previous: Strings

Thu Nov 13 16:44:05 EST 1997