home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


8-5 Nesting Records

In the “EmployeeRecord” example you may split the field address information to street address, city, state, and zip code. This means that the address field becomes a record nested in the “EmployeeRecord.” The new record will look as follows:

    TYPE
     AddressRecord = RECORD
                     Street           :STRING[18];
                     City     :STRING[15];
                     State            :STRING[2];
                     Zip      :String[5];
            END;
     EmployeeRecord = RECORD
                     Name             :STRING[25];
                     AddressRec       :AddressRecord;
                     Phone            :STRING[12];
                     Rate             :REAL;
                     MaritalStatus    :CHAR;
                    END;
    VAR
     EmployeeRec :EmployeeRecord;

In this declaration, you have two record types: “AddressRecord” and “EmployeeRecord.” The field “AddressRec” in the employee record is of the type “AddressRecord” which was defined before. To deal with any fielded variables in the “AddressRec” you have to attach both names of the two records “EmployeeRec” (which is the grandparent) and “AddressRec” (which is the parent). Here are some sample assignments:

    EmployeeRec.AddressRec.Street:= '15 Darell Street';
    EmployeeRec.AddressRec.Zip:= '60108';

When you display any of these fields you use the same method:

    WRITELN(EmployeeRec.AddressRec.Street);
    WRITELN(EmployeeRec.AddressRec.City);

Here is the complete program:

{ -------------------------- figure 8-4 -------------------------- }
PROGRAM NestedRecord(OUTPUT);
TYPE
 AddressRecord = RECORD
            Street  :STRING[18];
            City            :STRING[15];
            State   :STRING[2];
            Zip             :String[5];
           END;
 EmployeeRecord = RECORD
            Name                       :STRING[25];
            AddressRec                 :AddressRecord;
            Phone                      :STRING[12];
            Rate                       :REAL;
            MaritalStatus   :CHAR;
               END;
VAR
 EmployeeRec   :EmployeeRecord;
BEGIN
 EmployeeRec.Name:= 'Jean L. Krauss';
 EmployeeRec.AddressRec.Street:= '15 Darell Street';
 EmployeeRec.AddressRec.City:= 'Bloomingdale';
 EmployeeRec.AddressRec.State:= 'IL';
 EmployeeRec.AddressRec.Zip:= '60108';
 EmployeeRec.Phone:= '312-987-5432';
 EmployeeRec.Rate:= 27.5;
 EmployeeRec.MaritalStatus:= 'M';
 WRITELN('Employee Name:  ', EmployeeRec.Name);
 WRITELN('Address:   ', EmployeeRec.AddressRec.Street);
 WRITELN('         ', EmployeeRec.AddressRec.City);
 WRITE('         ', EmployeeRec.AddressRec.State);
 WRITELN(' ', EmployeeRec.AddressRec.Zip);
 WRITELN('Telephone #:    ', EmployeeRec.Phone);
 WRITELN('Hourly Rate:   $', EmployeeRec.Rate:0:2);
 WRITELN('Marital Status: ', EmployeeRec.MaritalStatus)
END.

The output is:

Employee Name:  Jean L. Krauss
Address:    15 Darell Street
          Bloomingdale
          IL 60108
Telephone #:     312-987-5432
Hourly Rate:    $27.50
Marital Status: M

If you would like to use the WITH statement with such a nested record you need two nested WITH blocks, thus:

    WITH EmployeeRec DO
     WITH AddressRec DO
           BEGIN
            Name:= 'Tammy M. Ockman';
            Street:= '344 Temple Dr.';
            ...
      END;

If any field identifier belongs to the “AddressRec,” it will be modified by both “AddressRec” and “EmployeeRec,” but if it belongs to the “EmployeeRec” directly, it will be modified by “EmployeeRec” only. If it is a regular variable, it will not be modified at all.

Drill 8-3

Write the complete program that initializes and displays the employee record using the WITH statement with the nested address record shown above.

Summary

In this chapter you have met two structured data types, the set and the record, and are now familiar with their features.

1.  You now know how to declare a set of a specific base type using the form:
    type-identifier = SET OF base-type;
2.  You also know the standard set operators (union (+), intersection (*), and difference (-)) and the set relational operators ( = >= <= <>), and learned how to use these operators to process sets.
3.  You are familiar with restrictions on sets, as well as their main uses in programming.
4.  You declare record types using the form
    RECORD
     filed-list
    END;
5.  You can access fields using either fielded variables or the WITH statement, which takes the form:
    WITH record-identifier DO
     statement;
6.  You also know how to declare and use nested records and how to process them as well.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.