home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / pascal / PAS_TUT.ZIP / CHAP09.TXT < prev    next >
Encoding:
Text File  |  1991-02-04  |  18.2 KB  |  413 lines

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