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.

 

        \RECORDS.XPL

        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;

 

 

        begin   \Main

        File:=[ [ ["WIRTH", "NIKLAUS"],

                   "701-25-9412",

                   Male,

                   [Aug, 30, 1944],

                   4,

                   Married              ],

 

                [ ["BOREAL", "LENNY"],

                   "521-54-1657",

                   Male,

                   [Oct, 22, 1948],

                   1,

                   Single               ],

 

                [ ["MUPPET", "PIGGY"],

                   "345-51-7734",

                   Female,

                   [Feb, 25, 1955],

                   1,

                   Single               ] ];

 

        for Person:= 0, 2 do

            if File(Person,Sex)=Female & File(Person,Status)=Single then

                begin

                Text(0, "MISS ");

                Text(0, File(Person,Name,First));

                ChOut(0 ,^ );

                Text(0, File(Person,Name,Last));

                CrLf(0);

                end;

        end;    \Main

 

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.