|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
8-5 Nesting RecordsIn 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.
SummaryIn this chapter you have met two structured data types, the set and the record, and are now familiar with their features.
|
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. |