home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / modula2 / mod2txt.zip / CHAP11.TXT < prev    next >
Text File  |  1987-03-25  |  20KB  |  454 lines

  1.                            CHAPTER 11 - Records
  2.  
  3.  
  4.                       PREREQUISITES FOR THIS MATERIAL
  5.  
  6.              In order to do a profitable study of this material, you
  7.         will  need  a good understanding of all of the  material  in
  8.         Part  I.   The  material  concerning the  scalar  type  from
  9.         chapter 11 is also needed.
  10.  
  11.             We  come  to  the grandaddy of all  data  structures  in
  12.         Modula-2,  the RECORD.   A record is composed of a number of
  13.         variables  any of which can be of any predefined data  type,
  14.         including  other records.   Rather than spend time trying to
  15.         define  a  record  in detail,  lets go right  to  the  first
  16.         example  program,  SMALLREC.MOD.   This is a  program  using
  17.         nonsense data that will illustrate the use of a record.
  18.  
  19.                            A VERY SIMPLE RECORD
  20.  
  21.             There is only one entry in the TYPE declaration part  of
  22.         the program,  namely the record identified by "Description".
  23.         The record is composed of three fields, the "Year", "Model",
  24.         and  "Engine" variables.   Notice that the three fields  are
  25.         each of a different type,  indicating that the record can be
  26.         of  mixed types.   You have a complete example of the way  a
  27.         record  is  defined  before  you.   It is  composed  of  the
  28.         identifier "Description", the reserved word RECORD, the list
  29.         of  elements,  and followed by END;.   Notice that this only
  30.         defines a TYPE,  it does not define any variables.   That is
  31.         done  in  the VAR declaration where the variable  "Cars"  is
  32.         defined   to   have  10  complete  records   of   the   type
  33.         "Description".  The variable "Cars[1]" has three components,
  34.         "Year",  "Model",  and  "Engine",  and  any or all of  these
  35.         components   can  be  used  to  store  data  pertaining   to
  36.         "Cars[1]".
  37.  
  38.             In  order  to assign values to the various  fields,  the
  39.         variable name is followed by the sub-field with a separating
  40.         period.   Keep  in mind that "Cars[1]" is a complete  record
  41.         containing three variables,  and to assign or use one of the
  42.         variables,  you  must  designate  which  sub-field  you  are
  43.         interested in.   See the program where the three fields  are
  44.         assigned  meaningless  data for  illustration.   The  "Year"
  45.         field  is  assigned  an  integer  number  varying  with  the
  46.         subscript,   all   "Model"  fields  are  assigned  the  name
  47.         "Duesenburg",  and  all "Engine" variables are assigned  the
  48.         value "V8".   In order to further illustrate that there  are
  49.         actually  30  variables in use here,  a few are  changed  at
  50.         random  in  the next few statements,  being very careful  to
  51.         maintain   the  required  types  as  defined  in  the   TYPE
  52.         declaration part of the program.  Finally, all ten composite
  53.         variables,  consisting  of 30 actual variables in a  logical
  54.         grouping  are  printed  out using  the  same  "var.subfield"
  55.  
  56.  
  57.                                   Page 65
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                            CHAPTER 11 - Records
  68.  
  69.  
  70.         notation described above.
  71.  
  72.             If the preceding description of a record is not clear in
  73.         your mind,  review it very carefully.  It's a very important
  74.         concept in Modula-2,  and you won't have a hope of a  chance
  75.         of understanding the next example until this one is clear.
  76.  
  77.                               A SUPER RECORD
  78.  
  79.             Examine   the   example  file  BIGREC.MOD  for  a   very
  80.         interesting  record.   First  we have  a  constant  defined.
  81.         Ignore  it  for the moment,  we will come back to it  later.
  82.         Within  the TYPE declaration we have three records  defined,
  83.         and upon close examination,  you will notice that the  first
  84.         two  records  are included as part of the definition of  the
  85.         third record.   The record identified as "Person",  actually
  86.         contains 8 variable definitions, three within the "FullName"
  87.         record,  two of its own, and three within the "Date" record.
  88.         This is a TYPE declaration and does not actually define  any
  89.         variables, that is done in the VAR part of the program.
  90.  
  91.             The  VAR  part  of the program  defines  some  variables
  92.         beginning  with the array of "Friend" containing 50 (because
  93.         of  the  constant definition in the CONST part)  records  of
  94.         "Person".   Since  "Person" defines 8 fields,  we  have  now
  95.         defined  8  times 50 = 400 separate and distinct  variables.
  96.         Each  of  the  400  separate  variables  has  its  own  type
  97.         associated with it,  and the compiler will generate an error
  98.         if  you try to assign any of those variables the wrong  type
  99.         of  data.   Since "Person" is a TYPE definition,  it can  be
  100.         used  to define more than one variable,  and in fact  it  is
  101.         used again to define three more records,  "Self",  "Mother",
  102.         and  "Father".   These three records are each composed of  8
  103.         variables,  so  we  have  24  more variables  which  we  can
  104.         manipulate within the program.  Finally we have the variable
  105.         "Index" defined as a simple CARDINAL type variable.   Notice
  106.         that if we desired,  we could also define a variable of type
  107.         "FullName" composed of 3 simple variables.
  108.  
  109.                     HOW TO MANIPULATE ALL OF THAT DATA
  110.  
  111.             In  the program we begin by assigning data to all of the
  112.         fields of "Self".   Examining the first three statements  of
  113.         the main program,  we see the construction we learned in the
  114.         last  example program being used,  namely the period between
  115.         descriptor fields.   The main record is named "Self", and we
  116.         are  interested  in the first part of it namely  the  "Name"
  117.         part  of the person record.   Since the "Name" part  of  the
  118.         person  record  is itself composed of three parts,  we  must
  119.         designate  which  part  of it we  are  interested  in.   The
  120.         complete  description "Self.Name.FirstName" is the  complete
  121.  
  122.  
  123.                                   Page 66
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                            CHAPTER 11 - Records
  134.  
  135.  
  136.         description  of  the first name of "Self" and is  the  first
  137.         assignment   statement  which  is  assigned  the   name   of
  138.         "Charley".   The next two fields are handled in the same way
  139.         and are self explanatory.
  140.  
  141.                         WHAT IS THE WITH STATEMENT?
  142.  
  143.             Continuing on to the fourth field, the "City", there are
  144.         only  two  levels  required because "City"  is  not  another
  145.         record definition.  The fourth field is therefore completely
  146.         defined   by  "Self.City".    Notice  the  "WITH  Self   DO"
  147.         statement.   This  is a shorthand notation used with  record
  148.         definitions to simplify coding.   From the BEGIN at the next
  149.         statement  to the matching END;  about 10 statements  later,
  150.         any  variables  within the "Self" record are used as  though
  151.         they had a "Self." in front of them.   It greatly simplifies
  152.         coding to be able to omit the leading identifier within  the
  153.         WITH  section  of  code.   You will  see  that  "City",  and
  154.         "State",   are   easily  assigned  values  without   further
  155.         reference to the "Self" variable.   When we get to the "Day"
  156.         part  of the birthday,  we are back to three levels and  the
  157.         complete  definition is "Self.Birthday.Day" but once  again,
  158.         the  "Self." part is taken care of automatically because  we
  159.         are still within the "WITH Self DO" area.
  160.  
  161.             To  illustrate  the WITH statement further,  another  is
  162.         introduced,  "WITH Birthday DO",  and an area is defined  by
  163.         the   BEGIN  END  pair.    Within  this  area  both  leading
  164.         identifiers  are handled automatically to  simplify  coding,
  165.         and  "Month" is equivalent to writing  "Self.Birthday.Month"
  166.         if both WITH statements were removed.   You may be wondering
  167.         how   many   levels  of  nesting  are  allowed   in   record
  168.         definitions.   There  doesn't appear to be a limit according
  169.         to the Modula-2 definition,  but we do get a hint at how far
  170.         it is possible to go.   In most implementations of Modula-2,
  171.         you  are  allowed  to have WITH statements  nested  to  nine
  172.         levels,  and  it would be worthless to nest WITH  statements
  173.         deeper  than the level of records.   Any  program  requiring
  174.         more  levels  than nine is probably far beyond the scope  of
  175.         your programming ability, and mine, for a long time.
  176.  
  177.             After assigning a value to the year,  the entire  record
  178.         of "Self" is defined, all eight variables.
  179.  
  180.                         SUPER-ASSIGNMENT STATEMENTS
  181.  
  182.             The   next  statement,   "Mother  :=  Self;"   is   very
  183.         interesting.   Since both of these are records, both are the
  184.         same type of record, and both therefore contain 8 variables,
  185.         Modula-2  is smart enough to recognize that,  and assign all
  186.         eight  values  contained  in  "Self"  to  the  corresponding
  187.  
  188.  
  189.                                   Page 67
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                            CHAPTER 11 - Records
  200.  
  201.  
  202.         variables of "Mother".   So after one statement, "Mother" is
  203.         completely  defined.   The  next statement assigns the  same
  204.         values  to the eight respective fields of "Father", and  the
  205.         next  two  lines assign all 50 "Friend" variables  the  same
  206.         data.   We have therefore generated 400 + 24 = 424  separate
  207.         pieces  of data so far in this program.   We could print  it
  208.         all out,  but since it is nonsense data, it would only waste
  209.         time and paper.  The next three lines write out three sample
  210.         pieces of the data for your inspection.
  211.  
  212.                          WHAT GOOD IS ALL OF THIS
  213.  
  214.             It should be obvious to you that what this program does,
  215.         even  though  the  data  is  nonsense,  appears  to  be  the
  216.         beginning of a database management program,  which indeed it
  217.         is.  It is a crude beginning, and has a long way to go to be
  218.         useful, but you should see a seed for a useful program.
  219.  
  220.             Now to go back to the CONST as promised.   The number of
  221.         friends was defined as 50 and used for the size of the array
  222.         and in the assignment loop near the end of the program.  You
  223.         can  now edit this number and see how big this database  can
  224.         become on your computer.  Your compiler should be capable of
  225.         storing  about  1000 records even within the smallest  model
  226.         available  on any compiler.  If your compiler uses a  larger
  227.         memory model,  you will be able to store significantly  more
  228.         records.   See  how  big you can make the number of  friends
  229.         before you get the memory overflow message.  Keep the number
  230.         in  mind because when we get to the chapter on Pointers  and
  231.         Dynamic  Allocation,  you  should see a marked  increase  in
  232.         allowable size, especially if you have a large amount of RAM
  233.         installed  in your computer.   If your compiler uses a large
  234.         memory model,  you won't see an increase in size but it will
  235.         be an interesting exercise anyway.
  236.  
  237.                              A VARIANT RECORD
  238.  
  239.             If  any part of this chapter is still unclear,  it would
  240.         be good for you to go back and review it at this time.   The
  241.         next  example  will  really  tax  your  mind  to  completely
  242.         understand  it,  especially  if the prior  material  is  not
  243.         clear.
  244.  
  245.             Examine  the  program  VARREC.MOD for an  example  of  a
  246.         program with a variant record definition.   In this example,
  247.         we  first define a scalar type,  namely "KindOfVehicle"  for
  248.         use  within  the record.   Then we have  a  record  defining
  249.         "Vehicle",  intended  to  define several different types  of
  250.         vehicles,  each with different kinds of data.   It would  be
  251.         possible  to define all variables for all types of vehicles,
  252.         but  it  would  be a waste of storage space  to  define  the
  253.  
  254.  
  255.                                   Page 68
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                            CHAPTER 11 - Records
  266.  
  267.  
  268.         number  of  tires for a boat,  or the  number  of  propeller
  269.         blades  used on a car or truck.   The variant record lets us
  270.         define  the data precisely for each vehicle without  wasting
  271.         data storage space.
  272.  
  273.                            WHAT IS A TAG-FIELD?
  274.  
  275.             In the record definition we have the usual RECORD header
  276.         followed  by three variables defined in the same  manner  as
  277.         the records in the last two example programs.   Then we come
  278.         to the CASE statement.  Following this statement, the record
  279.         is  different  for  each of the four types  defined  in  the
  280.         associated  scalar definition.   The variable "WhatKind"  is
  281.         called  the tag-field and must be defined as a  scalar  type
  282.         prior  to  the  record definition.   The tag-field  is  what
  283.         selects the variant used,  when the program uses one of  the
  284.         variables with this record type.   The tag-field is followed
  285.         by  a colon and its type definition,  then the reserved word
  286.         OF.   A list of the variants is then given, with each of the
  287.         variants  having  the  variables  for  its  particular  case
  288.         defined.   The  list of variables for one variant is  called
  289.         the field list.
  290.  
  291.             A few rules are in order at this point.  The variants do
  292.         not have to have the same number of variables in each  field
  293.         list,  and in fact,  one or more of the variants may have no
  294.         variables  at all in its variant part.   If a variant has no
  295.         variables, it must still be defined with a blank followed by
  296.         a semi-colon.  All variables in the entire variant part must
  297.         have unique names.   The three variables, "Wheels", "Tires",
  298.         and "Tyres",  all mean the same thing to the user,  but they
  299.         must  be different for the compiler.   You may use the  same
  300.         identifiers again in other records and for simple  variables
  301.         anywhere  else  in the program.   The Modula-2 compiler  can
  302.         tell which variable you mean by its context.  Using the same
  303.         variable  name  should  be discouraged  as  bad  programming
  304.         practice because it may confuse you or another person trying
  305.         to understand your program at a later date.
  306.  
  307.                          USING THE VARIANT RECORD
  308.  
  309.             We  properly define four variables with the record  type
  310.         "Vehicle" and go on to examine the program itself.
  311.  
  312.             We  begin  by  defining  one of our  variables  of  type
  313.         "Vehicle",  namely  the variable named  "Ford".   The  seven
  314.         lines  assigning  values to "Ford" are similar to the  prior
  315.         examples  with  the exception of the fourth  line.   In  the
  316.         fourth  line  the  tag-field which  selects  the  particular
  317.         variant used is set equal to the value "Truck",  which is  a
  318.         scalar  definition,  not  a variable.   This means that  the
  319.  
  320.  
  321.                                   Page 69
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                            CHAPTER 11 - Records
  332.  
  333.  
  334.         variables  named  "Motor",   "Tires",   and  "Payload"   are
  335.         available for use with the record "Ford",  but the variables
  336.         named "Wheels", "Engine", "Tyres", etc. are not available in
  337.         the record named "Ford".
  338.  
  339.             Next,  lets  define the record "Sunfish" as a boat,  and
  340.         define all of its variables.  All of sunfish's variables are
  341.         defined but in a rather random order to illustrate that they
  342.         need not be defined in a particular order.   Recall the  use
  343.         of WITH from the last example program.
  344.  
  345.             To  go even further in randomly assigning the  variables
  346.         to a record,  we redefine "Ford" as having an "Engine" which
  347.         it  can only have if it is a car.   This is one of the  fine
  348.         points  of  the record.   If you assign any of  the  variant
  349.         variables,  the record is changed to that variant, but it is
  350.         the  programmers responsibility to assign the  correct  tag-
  351.         field  to  the record,  not the Modula-2  compiler's.   Good
  352.         programming practice would be to assign the tag-field before
  353.         assigning  any of the variant variables.   The remainder  of
  354.         the  "Ford" variables are assigned to complete that  record,
  355.         the non-variant part remaining from the last assignment.
  356.  
  357.             The  variable  "Mac"  is now set equal to  the  variable
  358.         "Sunfish".   All  variables within the record are copied  to
  359.         "Mac" including the tag-field, making "Mac" a boat.
  360.  
  361.                   NOW TO SEE WHAT WE HAVE IN THE RECORDS
  362.  
  363.             We  have  assigned "Ford" to be a  car,  and  two  boats
  364.         exist,  namely  "Sunfish"  and "Mac".   Since "Schwinn"  was
  365.         never defined,  it has no data in it,  and is at this  point
  366.         useless.  The "Ford" tag-field has been defined as a car, so
  367.         it should be true in the IF statement, and the first message
  368.         should  print.   The "Sunfish" is not a bicycle,  so it will
  369.         not  print.   The  "Mac" has been defined as a boat  in  the
  370.         single assignment statement, so it will print a message with
  371.         an  indication  that  all  of the data  in  the  record  was
  372.         transferred to its variables.
  373.  
  374.             Even  though  we  can make  assignment  statements  with
  375.         records,  they cannot be used in any mathematical operations
  376.         such as addition,  or multiplication.   They are simply used
  377.         for data storage.   It is true however,  that the individual
  378.         elements  in  a  record  can be  used  in  any  mathematical
  379.         statements legal for their respective types.
  380.  
  381.             One other point should be mentioned.   The tag-field can
  382.         be completely eliminated resulting in a "free union" variant
  383.         record.   This  is  possible because Modula-2,  as  you  may
  384.         remember  from above,  will automatically assign the variant
  385.  
  386.  
  387.                                   Page 70
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.                            CHAPTER 11 - Records
  398.  
  399.  
  400.         required when you assign data to one of the variables within
  401.         a variant.  This is the reason that all variables within any
  402.         of  the  variants must have unique names.   The  free  union
  403.         record  should be avoided in your early programming  efforts
  404.         because you cannot test a record to see what variant it  has
  405.         been assigned to.
  406.  
  407.                         A NOTE TO PASCAL PROGRAMMERS
  408.  
  409.              A  record with a free union variant is commonly used in
  410.         Pascal to do type transfers,  but this should be discouraged
  411.         in Modula-2 since it has a complete set of carefully defined
  412.         type transfer functions for that purpose.   In addition, the
  413.         method  of  data storage is not specified as a part  of  the
  414.         language  and  a free union would not operate the  same  way
  415.         with  different  compilers if used for the purpose  of  type
  416.         transfer.
  417.  
  418.                            PROGRAMMING EXERCISE
  419.  
  420.         1.  Write  a simple program with a record to store the names
  421.             of five of your friends and display the names.
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.                                   Page 71
  454.