A record provides another way to build new data structures. Like a list a record is a collection of other objects. In a record the elements are not indexed by numbers but by names (i.e., identifiers). An entry in a record is called a record component (or sometimes also record field).
gap> date:= rec(year:= 1992, > month:= "Jan", > day:= 13); rec( year := 1992, month := "Jan", day := 13 )
Initially a record is defined as a comma separated list of assignments to its record components. Then the value of a record component is accessible by the record name and the record component name separated by one dot as the record component selector.
gap> date.year; 1992 gap> date.time:= rec(hour:= 19, minute:= 23, second:= 12); rec( hour := 19, minute := 23, second := 12 ) gap> date; rec( year := 1992, month := "Jan", day := 13, time := rec( hour := 19, minute := 23, second := 12 ) )
Assignments to new record components are possible in the same way. The record is automatically resized to hold the new component.
Most of the complex structures that are handled by GAP are represented as records, for instance groups and character tables.
Records are objects that may be changed. An assignment to a record
component changes the original object. There are many functions in the
library that will do such assignments to a record component of one of
their arguments. The function Size
for example, will compute the size
of its argument which may be a group for instance, and then store the
value in the record component size
. The next call of Size
for this
object will use this stored value rather than compute it again.
Lists and records are the only types of GAP objects that can be changed.
Sometimes it is interesting to know which components of a certain record
are bound. This information is available from the function RecFields
(yes, this function should be called RecComponentNames
), which takes a
record as its argument and returns a list of all bound components of this
record as a list of strings.
gap> RecFields(date); [ "year", "month", "day", "time" ]
Finally try the following examples and explain the results.
gap> r:= rec(); rec( ) gap> r:= rec(r:= r); rec( r := rec( ) ) gap> r.r:= r; rec( r := ~ )
Now return to section About Identical Lists and find out what that section means for records.
In this section you have seen how to define and how to use records. Record objects are changed by assignments to record fields. Lists and records are the only types of objects that can be changed.
Records and functions for records are described in detail in chapter Records. More about identical records is found in Identical Records.
GAP 3.4.4