home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / PASCTUT1.ZIP / CHAP9.TXT < prev    next >
Encoding:
Text File  |  1986-07-14  |  19.9 KB  |  455 lines

  1.                            CHAPTER 9 - Records
  2.  
  3.  
  4.             We  come  to  the grandaddy of all  data  structures  in 
  5.  
  6.         Pascal,  the  RECORD.   A record is composed of a number  of 
  7.  
  8.         variables  any of which can be of any predefined data  type, 
  9.  
  10.         including  other records.   Rather than spend time trying to 
  11.  
  12.         define  a  record  in detail,  lets go right  to  the  first 
  13.  
  14.         example program, SMALLREC.  This is a program using nonsense 
  15.  
  16.         data that will illustrate the use of a record.
  17.  
  18.                            A VERY SIMPLE RECORD
  19.  
  20.             There is only one entry in the TYPE declaration part  of 
  21.  
  22.         the program,  namely the record identified by "description".  
  23.  
  24.         The record is composed of three fields, the "year", "model", 
  25.  
  26.         and  "engine" variables.   Notice that the three fields  are 
  27.  
  28.         each of a different type,  indicating that the record can be 
  29.  
  30.         of  mixed types.   You have a complete example of the way  a 
  31.  
  32.         record  is  defined  before  you.   It is  composed  of  the 
  33.  
  34.         identifier ("description"),  the reserved word  RECORD,  the 
  35.  
  36.         list of elements,  and followed by END;.  This is one of the 
  37.  
  38.         places   in   Pascal  where  an  END  is  used   without   a 
  39.  
  40.         corresponding BEGIN.   Notice that this only defines a TYPE, 
  41.  
  42.         it  does not define any variables.   That is done in the VAR 
  43.  
  44.         declaration where the variable "cars" is defined to have  10 
  45.  
  46.         complete  records of the type "description".   The  variable 
  47.  
  48.         "cars[1]" has three components, year, model, and engine, and 
  49.  
  50.         any  or  all of these components can be used to  store  data 
  51.  
  52.         pertaining to "cars[1]".
  53.  
  54.             In  order  to assign values to the various  fields,  the 
  55.  
  56.         variable name is followed by the sub-field with a separating 
  57.  
  58.         period.   Keep  in mind that "cars[1]" is a complete  record 
  59.  
  60.         containing three variables,  and to assign or use one of the 
  61.  
  62.         variables,  you  must  designate  which  sub-field  you  are 
  63.  
  64.         interested in.   See the program where the three fields  are 
  65.  
  66.         assigned  meaningless  data for  illustration.   The  "year" 
  67.  
  68.         field  is  assigned  an  integer  number  varying  with  the 
  69.  
  70.         subscript,   all   "model"  fields  are  assigned  the  name 
  71.  
  72.         "Duesenburg",  and  all "engine" variables are assigned  the 
  73.  
  74.         value "V8".   In order to further illustrate that there  are 
  75.  
  76.         actually  30  variables in use here,  a few are  changed  at 
  77.  
  78.         random  in  the next few statements,  being very careful  to 
  79.  
  80.         maintain   the  required  types  as  defined  in  the   TYPE 
  81.  
  82.         declaration part of the program.  Finally, all ten composite 
  83.  
  84.         variables,  consisting  of 30 actual variables in a  logical 
  85.  
  86.         grouping  are  printed  out using  the  same  "var.subfield" 
  87.  
  88.         notation described above.
  89.  
  90.             If the preceding description of a record is not clear in 
  91.  
  92.         your mind,  review it very carefully.  It's a very important 
  93.  
  94.         concept in Pascal,  and you won't have a hope of a chance of 
  95.  
  96.         understanding the next example until this one is clear.
  97.  
  98.  
  99.  
  100.                                   Page 39
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.                            CHAPTER 9 - Records
  111.  
  112.  
  113.  
  114.                               A SUPER RECORD
  115.  
  116.             Examine  the  Pascal  example file  BIGREC  for  a  very 
  117.  
  118.         interesting  record.   First  we  have a  constant  defined.  
  119.  
  120.         Ignore  it for the moment,  we will come back to  it  later.  
  121.  
  122.         Within  the TYPE declaration we have three records  defined, 
  123.  
  124.         and  upon close examination,  you will notice that the first 
  125.  
  126.         two  records are included as part of the definition  of  the 
  127.  
  128.         third record.   The record identified as "person",  actually 
  129.  
  130.         contains   9   variable  definitions,   three   within   the 
  131.  
  132.         "full_name" record,  three of its own,  and three within the 
  133.  
  134.         "date"  record.   This  is a TYPE declaration and  does  not 
  135.  
  136.         actually define any variables,  that is done in the VAR part 
  137.  
  138.         of the program.
  139.  
  140.             The  VAR  part  of the program  defines  some  variables 
  141.  
  142.         beginning  with the array of "friend" containing 50 (because 
  143.  
  144.         of  the  constant definition in the CONST part)  records  of 
  145.  
  146.         "person".   Since  "person" defines 9 fields,  we  have  now 
  147.  
  148.         defined  9  times 50 = 450 separate and distinct  variables, 
  149.  
  150.         each  with its own defined type.   Remember that  Pascal  is 
  151.  
  152.         picky about assigning data by the correct type.  Each of the 
  153.  
  154.         450  separate variables has its own type associated with it, 
  155.  
  156.         and the compiler will generate an error if you try to assign 
  157.  
  158.         any  of  those  variables the wrong  type  of  data.   Since 
  159.  
  160.         "person" is a TYPE definition, it can be used to define more 
  161.  
  162.         than  one variable,  and in fact it is used again to  define 
  163.  
  164.         three more records,  "self",  "mother", and "father".  These 
  165.  
  166.         three records are each composed of 9 variables,  so we  have 
  167.  
  168.         27  more  variables  which  we  can  manipulate  within  the 
  169.  
  170.         program.   Finally we have the variable "index" defined as a 
  171.  
  172.         simple byte type variable.
  173.  
  174.                     HOW TO MANIPULATE ALL OF THAT DATA
  175.  
  176.             In  the program we begin by assigning data to all of the 
  177.  
  178.         fields of "self".   Examining the first three statements  of 
  179.  
  180.         the main program,  we see the construction we learned in the 
  181.  
  182.         last  example program being used,  namely the period between 
  183.  
  184.         descriptor fields.   The main record is named "self", and we 
  185.  
  186.         are  interested  in the first part of it namely  the  "name" 
  187.  
  188.         part  of the person record.   Since the "name" part  of  the 
  189.  
  190.         person  record  is itself composed of three parts,  we  must 
  191.  
  192.         designate  which  part  of it we  are  interested  in.   The 
  193.  
  194.         complete description "self.name.first_name" is the  complete 
  195.  
  196.         description  of  the first name of "self" and is  the  first 
  197.  
  198.         assignment   statement   which  is  assigned  the  name   of 
  199.  
  200.         "Charley".   The next two fields are handled in the same way 
  201.  
  202.         and are self explanatory.
  203.  
  204.  
  205.  
  206.  
  207.                                   Page 40
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.                            CHAPTER 9 - Records
  218.  
  219.  
  220.                         WHAT IS THE WITH STATEMENT?
  221.  
  222.             Continuing on to the fourth field, the "city", there are 
  223.  
  224.         only  two  levels  required because "city"  is  not  another 
  225.  
  226.         record definition.  The fourth field is therefore completely 
  227.  
  228.         defined   by  "self.city".    Notice  the  "WITH  self   DO" 
  229.  
  230.         statement.   This  is a shorthand notation used with  record 
  231.  
  232.         definitions to simplify coding.   From the BEGIN at the next 
  233.  
  234.         statement  to the matching END;  about 10 statements  later, 
  235.  
  236.         any  variables within the "self" record are used  as  though 
  237.  
  238.         they had a "self." in front of them.   It greatly simplifies 
  239.  
  240.         coding  to be able to omit the leading identifier within the 
  241.  
  242.         WITH section of code.   You will see that  "city",  "state", 
  243.  
  244.         and  "zipcode"  are easily assigned values  without  further 
  245.  
  246.         reference to the "self" variable.   When we get to the "day" 
  247.  
  248.         part  of the birthday,  we are back to three levels and  the 
  249.  
  250.         complete  definition is "self.birthday.day" but once  again, 
  251.  
  252.         the  "self." part is taken care of automatically because  we 
  253.  
  254.         are still within the "WITH self DO" area.
  255.  
  256.             To  illustrate  the WITH statement further,  another  is 
  257.  
  258.         introduced namely "WITH birthday DO", and an area is defined 
  259.  
  260.         by  the  BEGIN  END pair.   Within this  area  both  leading 
  261.  
  262.         identifiers  are handled automatically to  simplify  coding, 
  263.  
  264.         and  "month" is equivalent to writing  "self.birthday.month" 
  265.  
  266.         if both WITH statements were removed.   You may be wondering 
  267.  
  268.         how   many   levels  of  nesting  are  allowed   in   record 
  269.  
  270.         definitions.   There  doesn't appear to be a limit according 
  271.  
  272.         to the Pascal definition, but we do get a hint at how far it 
  273.  
  274.         is possible to go.  In TURBO Pascal, you are allowed to have 
  275.  
  276.         WITH  statements  nested to nine levels,  and  it  would  be 
  277.  
  278.         worthless  to nest WITH statements deeper than the level  of 
  279.  
  280.         records.   Any  program  requiring more levels than nine  is 
  281.  
  282.         probably  far beyond the scope of your programming  ability, 
  283.  
  284.         and  mine,  for a long time.   Pascal implementations  other 
  285.  
  286.         than  TURBO  Pascal probably have their own  WITH  statement 
  287.  
  288.         nesting limitation. Check your reference manual.
  289.  
  290.             After assigning a value to the year,  the entire  record 
  291.  
  292.         of "self" is defined, all nine variables.
  293.  
  294.                         SUPER-ASSIGNMENT STATEMENTS
  295.  
  296.             The   next   statement,   "mother  :=  self;"  is   very 
  297.  
  298.         interesting.   Since both of these are records, both are the 
  299.  
  300.         same type of record, and both therefore contain 9 variables, 
  301.  
  302.         Pascal  is smart enough to recognize that,  and  assign  all 
  303.  
  304.         nine   values  contained  in  "self"  to  the  corresponding 
  305.  
  306.         variables of "mother".   So after one statement, "mother" is 
  307.  
  308.         completely  defined.   The next statement assigns  the  same 
  309.  
  310.         values  to the nine respective fields of "father",  and  the 
  311.  
  312.  
  313.  
  314.                                   Page 41
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.                            CHAPTER 9 - Records
  325.  
  326.  
  327.         next  two  lines assign all 50 "friend" variables  the  same 
  328.  
  329.         data.   We  have therefore generated 450 + 27 = 477 separate 
  330.  
  331.         pieces  of data so far in this program.   We could print  it 
  332.  
  333.         all out,  but since it is nonsense data, it would only waste 
  334.  
  335.         time and paper.  The next three lines write out three sample 
  336.  
  337.         pieces of the data for your inspection.
  338.  
  339.                          WHAT GOOD IS ALL OF THIS
  340.  
  341.             It should be obvious to you that what this program does, 
  342.  
  343.         even  though  the  data  is  nonsense,  appears  to  be  the 
  344.  
  345.         beginning of a database management program,  which indeed it 
  346.  
  347.         is.  It is a crude beginning, and has a long way to go to be 
  348.  
  349.         useful, but you should see a seed for a useful program.
  350.  
  351.             Now to go back to the CONST as promised.   The number of 
  352.  
  353.         friends was defined as 50 and used for the size of the array 
  354.  
  355.         and in the assignment loop near the end of the program.  You 
  356.  
  357.         can  now edit this number and see how big this database  can 
  358.  
  359.         become on your computer.  If you are using TURBO Pascal, you 
  360.  
  361.         will  be limited to slightly more than 1000 because  of  the 
  362.  
  363.         64K  limitation of an executable program,  and the fact that 
  364.  
  365.         all  of this data is stored within that 64K  boundary.   See 
  366.  
  367.         how  big you can make the number of friends before  you  get 
  368.  
  369.         the  memory  overflow  message.   Keep the  number  in  mind 
  370.  
  371.         because  when we get to the chapter on Pointers and  Dynamic 
  372.  
  373.         Allocation,  you  will  see a marked increase  in  allowable 
  374.  
  375.         size, especially if you have a large amount of RAM installed 
  376.  
  377.         in your computer.
  378.  
  379.                              A VARIANT RECORD
  380.  
  381.             If  any part of this chapter is still unclear,  it would 
  382.  
  383.         be good for you to go back and review it at this time.   The 
  384.  
  385.         next  example  will  really  tax  your  mind  to  completely 
  386.  
  387.         understand  it,  especially  if the prior  material  is  not 
  388.  
  389.         clear.
  390.  
  391.             Examine  the  Pascal program VARREC for an example of  a 
  392.  
  393.         program with a variant record definition.   In this example, 
  394.  
  395.         we first define a scalar type,  namely "kind_of_vehicle" for 
  396.  
  397.         use  within  the record.   Then we have  a  record  defining 
  398.  
  399.         "vehicle",  intended  to  define several different types  of 
  400.  
  401.         vehicles,  each with different kinds of data.   It would  be 
  402.  
  403.         possible  to define all variables for all types of vehicles, 
  404.  
  405.         but  it  would  be a waste of storage space  to  define  the 
  406.  
  407.         number  of  tires for a boat,  or the  number  of  propeller 
  408.  
  409.         blades  used on a car or truck.   The variant record lets us 
  410.  
  411.         define  the data precisely for each vehicle without  wasting 
  412.  
  413.         data storage space.
  414.  
  415.  
  416.  
  417.  
  418.                                   Page 42
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.                            CHAPTER 9 - Records
  429.  
  430.  
  431.                            WHAT IS A TAG-FIELD?
  432.  
  433.             In the record definition we have the usual RECORD header 
  434.  
  435.         followed  by three variables defined in the same  manner  as 
  436.  
  437.         the records in the last two example programs.   Then we come 
  438.  
  439.         to the CASE statement.  Following this statement, the record 
  440.  
  441.         is  different  for  each of the four types  defined  in  the 
  442.  
  443.         associated  scalar definition.   The variable "what_kind" is 
  444.  
  445.         called  the tag-field and must be defined as a  scalar  type 
  446.  
  447.         prior  to  the  record definition.   The tag-field  is  what 
  448.  
  449.         selects the variant used,  when the program uses one of  the 
  450.  
  451.         variables with this record type.   The tag-field is followed 
  452.  
  453.         by  a colon and its type definition,  then the reserved word 
  454.  
  455.         OF.   A list of the variants is then given, with each of the 
  456.  
  457.         variants  having  the  variables  for  its  particular  case 
  458.  
  459.         defined.   The  list of variables for one variant is  called 
  460.  
  461.         the field list.
  462.  
  463.             A few rules are in order at this point.  The variants do 
  464.  
  465.         not have to have the same number of variables in each  field 
  466.  
  467.         list,  and in fact,  one or more of the variants may have no 
  468.  
  469.         variables  at all in its variant part.   If a variant has no 
  470.  
  471.         variables,  it  must still be defined with a pair  of  empty 
  472.  
  473.         parentheses followed by a semi-colon.   All variables in the 
  474.  
  475.         entire  variant  part  must have unique  names.   The  three 
  476.  
  477.         variables, "wheels", "tires", and "tyres", all mean the same 
  478.  
  479.         thing  to  the  user,  but they must be  different  for  the 
  480.  
  481.         compiler.   You may use the same identifiers again in  other 
  482.  
  483.         records  and  for  simple  variables anywhere  else  in  the 
  484.  
  485.         program.   The  Pascal compiler can tell which variable  you 
  486.  
  487.         mean by its context.  Using the same variable name should be 
  488.  
  489.         discouraged  as  bad  programming practice  because  it  may 
  490.  
  491.         confuse  you  or another person trying  to  understand  your 
  492.  
  493.         program at a later date.  The final rule is that the variant 
  494.  
  495.         part of the record must be the last part of it, and in fact, 
  496.  
  497.         the  last  part  of any or all variants can  itself  have  a 
  498.  
  499.         variant part to it.  That is getting pretty advanced for our 
  500.  
  501.         level of use of Pascal at this time however.
  502.  
  503.                          USING THE VARIANT RECORD
  504.  
  505.             We  properly define four variables with the record  type 
  506.  
  507.         and go on to examine the program itself.
  508.  
  509.             We  begin  by  defining  one of our  variables  of  type 
  510.  
  511.         "vehicle",  namely  the variable named  "ford".   The  seven 
  512.  
  513.         lines  assigning  values to "ford" are similar to the  prior 
  514.  
  515.         examples  with  the exception of the fourth  line.   In  the 
  516.  
  517.         fourth  line  the  tag-field which  selects  the  particular 
  518.  
  519.         variant used is set equal to the value "truck",  which is  a 
  520.  
  521.         scalar  definition,  not  a variable.   This means that  the 
  522.  
  523.  
  524.  
  525.                                   Page 43
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.                            CHAPTER 9 - Records
  536.  
  537.  
  538.         variables  named  "motor",   "tires",   and  "payload"   are 
  539.  
  540.         available for use with the record "ford",  but the variables 
  541.  
  542.         named "wheels", "engine", "tyres", etc. are not available in 
  543.  
  544.         the record named "ford".
  545.  
  546.             Next,  lets  define the record "sunfish" as a boat,  and 
  547.  
  548.         define all of its variables.  All of sunfish's variables are 
  549.  
  550.         defined but in a rather random order to illustrate that they 
  551.  
  552.         need not be defined in a particular order.
  553.  
  554.             To  go even further in randomly assigning the  variables 
  555.  
  556.         to a record,  we redefine "ford" as having an "engine" which 
  557.  
  558.         it  can only have if it is a car.   This is one of the  fine 
  559.  
  560.         points  of  the Pascal record.   If you assign  any  of  the 
  561.  
  562.         variant  variables,  the record is changed to that  variant, 
  563.  
  564.         but  it  is  the programmers responsibility  to  assign  the 
  565.  
  566.         correct  tag-field  to  the  record,   not  Pascal's.   Good 
  567.  
  568.         programming practice would be to assign the tag-field before 
  569.  
  570.         assigning  any of the variant variables.   The remainder  of 
  571.  
  572.         the  "ford" variables are assigned to complete that  record, 
  573.  
  574.         the non-variant part remaining from the last assignment.
  575.  
  576.             The  variable  "mac"  is now set equal to  the  variable 
  577.  
  578.         "sunfish".   All  variables within the record are copied  to 
  579.  
  580.         "mac" including the tag-field, making "mac" a boat.
  581.  
  582.                   NOW TO SEE WHAT WE HAVE IN THE RECORDS
  583.  
  584.             We  have  assigned "ford" to be a  car,  and  two  boats 
  585.  
  586.         exist,  namely  "sunfish"  and "mac".   Since "schwinn"  was 
  587.  
  588.         never defined,  it has no data in it,  and is at this  point 
  589.  
  590.         useless.  The "ford" tag-field has been defined as a car, so 
  591.  
  592.         it should be true in the IF statement, and the first message 
  593.  
  594.         should  print.   The "sunfish" is not a bicycle,  so it will 
  595.  
  596.         not  print.   The  "mac" has been defined as a boat  in  the 
  597.  
  598.         single assignment statement, so it will print a message with 
  599.  
  600.         an  indication  that  all  of the data  in  the  record  was 
  601.  
  602.         transferred to its variables.
  603.  
  604.             Even  though  we  can make  assignment  statements  with 
  605.  
  606.         records,  they cannot be used in any mathematical operations 
  607.  
  608.         such as addition,  or multiplication.   They are simply used 
  609.  
  610.         for data storage.   It is true however,  that the individual 
  611.  
  612.         elements  in  a  record  can be  used  in  any  mathematical 
  613.  
  614.         statements legal for their respective types.
  615.  
  616.             One other point should be mentioned.   The tag-field can 
  617.  
  618.         be completely eliminated resulting in a "free union" variant 
  619.  
  620.         record.   This  is  possible  because  Pascal,  as  you  may 
  621.  
  622.         remember from above,  will automatically assign the  variant 
  623.  
  624.         required when you assign data to one of the variables within 
  625.  
  626.  
  627.  
  628.                                   Page 44
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.                            CHAPTER 9 - Records
  639.  
  640.  
  641.         a variant.  This is the reason that all variables within any 
  642.  
  643.         of  the  variants must have unique names.   The  free  union 
  644.  
  645.         record  should be avoided in your early programming  efforts 
  646.  
  647.         because  you cannot test a record to see what variant it has 
  648.  
  649.         been assigned to.  It is definitely an advanced technique in 
  650.  
  651.         Pascal.
  652.  
  653.                            PROGRAMMING EXERCISE
  654.  
  655.         1.  Write  a simple program with a record to store the names      
  656.  
  657.             of five of your friends and display the names.
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.                                   Page 45
  701.  
  702.