Example Program: RECORDS (Advanced)
|Top Previous Next|
Because of the flexibility of XPL0 arrays, record structures can be made. A record structure is an array that contains elements of different types. In XPL0, integers and reals cannot both appear in a single array. However, integer values can be used to represent such diverse things as numbers, addresses of strings, and elements of a set.
Here is a program that combines the concept of sets with constant arrays and complex data structures.
code ChOut=8, CrLf=9, Text=12;
int File, Person;
def \Person\ Name, SS, Sex, Birth, Dependents, Status;
def \Name\ Last, First;
def \Sex\ Male, Female;
def \Birth\ Month, Day, Year;
def \Status\ Married, Widowed, Divorced, Single;
def \Month\ Jan, Feb, Mar, Apr, May, Jun,
Jul, Aug, Sep, Oct, Nov, Dec;
File:=[ [ ["WIRTH", "NIKLAUS"],
[Aug, 30, 1944],
[ ["BOREAL", "LENNY"],
[Oct, 22, 1948],
[ ["MUPPET", "PIGGY"],
[Feb, 25, 1955],
Single ] ];
for Person:= 0, 2 do
if File(Person,Sex)=Female & File(Person,Status)=Single then
Text(0, "MISS ");
ChOut(0 ,^ );
This program scans File for nubile females (and old maids) and produces the following output:
MISS PIGGY MUPPET
The program begins by defining the elements of the set Person. The elements that describe Person are: Name, social security number (SS), Sex, date of Birth, number of Dependents, and marital Status. Some of these elements are in turn defined as consisting of sub-elements. Name, for instance, consists of a Last name and a First name.
All these elements are mapped into the locations of the constant array called "File". The "def" declaration provides names for these locations (subscripts): Name=0, SS=1, Sex=2, etc. File consists of three major elements, or records, of "data type" Person.