A record provides another way to build new data structures. Like a list a record contains subobjects. In a record the elements, the so-called record components, are not indexed by numbers but by names.
In this section you will see how to define and how to use records. Records are changed by assignments to record components.
Initially a record is defined as a comma separated list of assignments to its record components.
gap> date:= rec(year:= 1997, > month:= "Jul", > day:= 14); rec( year := 1997, month := "Jul", day := 14 )
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; 1997
Assignments to new record components are possible in the same way. The record is automatically resized to hold the new component.
gap> date.time:= rec(hour:= 19, minute:= 23, second:= 12);
rec(
hour := 19,
minute := 23,
second := 12 )
gap> date;
rec(
year := 1997,
month := "Jul",
day := 14,
time := rec(
hour := 19,
minute := 23,
second := 12 ) )
Records are objects that may be changed. An assignment to a record component changes the original object. The remarks made in Sections Identical Lists and Immutability about identity and mutability of lists are also true for records.
Sometimes it is interesting to know which components of a certain record
are bound. This information is available from the function RecNames,
which takes a record as its argument and returns a list of names of
all bound components of this record as a list of strings.
gap> RecNames(date); [ "year", "month", "day", "time" ]
Now return to Sections Identical Lists and Immutability and find out what these sections mean for records.
[Top] [Previous] [Up] [Next] [Index]
GAP 4 manual