IPT - A Virtual Approach IPT A Virtual Approach by Peter Whitehouse
Quick Links:
 
 
Information and Intelligent Systems Social and Ethical Implications Human Computer Interaction Software and Systems Engineering eXercise Files Course Outline and Assessment A-Z of Geeky Acronyms Terrace Work Program 2004 Sillybus FAQ = Frequently Asked Questions Help
 
 

Pascal Records

Compound Data Structures

Database Structures

PASCAL RECORDS

Set, array, string - all organised collections of data of the SAME type.

Sometimes it is useful to record together related information that have differing types - use a RECORD

      syntax      type   name =      record
                                        field1 : type;
                                        field2 : type;
                                        field3 : type;
                                        fieldn : type
                                     end

Where fieldn are the cell names, and type is the data style of that cell.

Notice with this structure, that a record is similar to a vector, but with a major difference - records allow us to store mixed data types together.

   eg:   Type blackbookentrys =      record
                                        name      :  string[30];
                                        stdcode   :  string[4];
                                        phnum     :  string[10];
                                        yrlevel   :  8..12
                                     end;

         Var bbentry : blackbookentrys;

The above definition and declaration would be useful if you wanted to store a SINGLE friends details

Single records are rarely encountered - more commonly used are collections (arrays) of records

Accessing each of the fields in a record requires a new reference scheme. Using the above example to assign values to each of the fields, the following code could be used:

         Begin
             bbentry.name     := "Olivia Paarts";
             bbentry.stdcode  := "3214";
             bbentry.phnum    := "30001234";
             bbentry.yrlevel  := 9;

Note in the above assignment statements, the name of each field was explicitly referenced (ie. the container name and the field name were declared). Note also that a dot (.) separated the container name and the field name.

Introducing a Context

A major inconvenience is introduced in the previous example - the necessity to repeatedly retype the container name as part of a field reference. This can be averted by introducing a reference context:

            with bbentry do
               begin
                   name     := "Olivia Paarts";
                   stdcode  := "3214";
                   phnum    := "30001234";
                   yrlevel  := 9;
               end

The with statement allows us to pre-declare the container name, thus placing us within the context of the container. Once inside that container, the field names can be referred to without ambiguity.


Tables

The most common application of records is to group them into arrays. One could imagine that an array of records is similar to a table (with like fields aligned into columns, each record forming a row of the table

A TABLE CASE STUDY - An Army

Let us suppose that we wish to record details about a number of sodliers (including name, rank, serial number and IQ). Declarations similar tot he following could be used:

       Const  maxSoldiers = 100;
       Type   soldiers    = record
                              name    :  string[30];
                              rank    :  string[5];
                              serNum  :  byte;  {a small army}
                              IQ      :  byte 
                            end; {soldier}

              armies      = array[1..maxSoldiers] of soldiers; 

       Var    army : armies;
              loop : 1..maxSoldiers;
              temp : integer;

The armies data type is a table structure - each row represents an individual soldier

To enter a particular soldiers data (say soldier # 3), we could use the following assignments:

       Begin
             army[3].name     := "Olivia Paarts";
             army[3].rank     := "Priv";
             army[3].serNum   := 42;
             army[3].IQ       := 100;

..or conversely

       Begin
             with army[3] do
               begin
                 name     := "Olivia Paarts";
                 rank     := "Priv";
                 serNum   := 42;
                 IQ       := 100;
               end;

Because tables (or arrays of records) are arrays, it is possible to deal with their contents sequentially. Let us suppose that our army is FULL (ie. all the soldiers have heen assigned) - to print ot the names of all soldiers stored, the following code could be used:

             for loop := 1 to maxSoldiers do
                showmessage(army[loop].name)

Suppose we needed to calculate the average IQ of our army, using the integer temp variable, we could perform the following code segment:

             temp   := 0;
             for  loop := 1 to maxSoldiers do
                 temp := temp + army[loop].IQ;
             showmessage('average IQ = '+ floattostr(temp/maxSoldiers))
Compound Data Structures - RECORDS
 

wonko@wonko.info
©Copyright t 1992..2018+. Edition 26.150117
wonkosite
Creative Commons License
This work is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 2.1 Australia License
.