home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_oth / comal.lzh / COMAL / INFOTEXT / RECORDS < prev    next >
Encoding:
Text File  |  1991-08-16  |  7.3 KB  |  277 lines

  1. RECORDS - BRIEF INTRODUCTION
  2. by David Stidolph
  3.  
  4. [Taken from COMAL TODAY #25 with permision]
  5.  
  6. AmigaCOMAL not only implements Common COMAL, but
  7. extends it with many new features. This article
  8. focuses on one of those new features: RECORDS. A
  9. record is a set of data elements tied together
  10. under the same name. An example of this is a simple
  11. data base to keep track of a persons name, address
  12. and telephone number. In any other COMAL you might
  13. use the following variables:
  14.  
  15.     DIM last'name$ OF 20
  16.     DIM first'name$ OF 15
  17.     DIM inital$ OF 1
  18.     DIM address$ OF 25
  19.     DIM city$ OF 20
  20.     DIM state$ OF 2
  21.     DIM zip$ OF 5
  22.     DIM phone'num$ OF 15
  23.  
  24. This would work, but to pass a persons information
  25. to a procedure would require eight parameters -- a
  26. lot of typing at the very least. In AmigaCOMAL you
  27. could group all these variables into one record.
  28. For example:
  29.  
  30.     RECORD type'person@
  31.       FIELD last'name$ OF 20
  32.       FIELD first'name$ OF 15
  33.       FIELD inital$ OF 1
  34.       FIELD address$ OF 25
  35.       FIELD city$ OF 20
  36.       FIELD state$ OF 2
  37.       FIELD zip$ OF 5
  38.       FIELD phone'num$ OF 15
  39.     ENDRECORD type'person@
  40.  
  41. This may be a bit more typing initially, but let's
  42. suppose you need a procedure to enter information
  43. about a new person, but want to pass the variables
  44. as parameters because you want several different
  45. persons data in memory at once (the old person, the
  46. new person and matching data for comparisons).
  47. Under most COMAL's you would have a procedure
  48. declaration like this:
  49.  
  50.     PROC input'data(REF ln$,REF fn$,REF in$,REF
  51.   ad$,REF ci$,REF st$,REF zip$,REF ph$) //wrap line
  52.  
  53. Besides shortening the names so they would fit on
  54. a line, you would have to keep constant track of
  55. not only the names of the variables, but THE ORDER
  56. TO PASS THEM to the procedure. If you need several
  57. different versions of the same data, the names
  58. would start to get very long (like OLD'LAST'NAME$).
  59.  
  60. With AmigaCOMAL you can group all the different
  61. variables (called FIELDS) into a RECORD structure
  62. and pass it to the procedure like this:
  63.  
  64.   PROC input'data(REF person@)
  65.  
  66. Notice that you don't have to remember the order of
  67. parameters. To refer to any one of the variables
  68. kept in PERSON@ you use the following method:
  69.  
  70.   person@.last'name$
  71.  
  72. PERSON@. as a prefix to the variable informs COMAL
  73. that this is a record variable, and specifys the
  74. specific record (PERSON@). After that comes the
  75. actual variable you want to access. In order to use
  76. PERSON@ you must first define it (like
  77. DIMmensioning a string):
  78.  
  79.   DIM person@ OF type'person@
  80.  
  81. This tells COMAL that you want a variable record
  82. named PERSON@ and to look at the definition
  83. TYPE'PERSON@ for the different type of variables
  84. and their names. Another advantage is that you
  85. could create several different records based on
  86. TYPE'PERSON@ and make your program both shorter and
  87. more readable:
  88.  
  89.   DIM person@ OF type'person@
  90.   DIM old'person@ OF type'person@
  91.   DIM new'person@ OF type'person@
  92.  
  93. All of these records have the same named variables
  94. within them, but each of those variables could hold
  95. a different value (PERSON@.LAST'NAME$ is different
  96. from OLD'PERSON@.LAST'NAME$).
  97.  
  98. Besides making parameter passing easier, records
  99. can be assigned the values of other records if they
  100. are of the same type. For example, you may wish to
  101. make a new person the current one, but first change
  102. the current one to be the old'person:
  103.  
  104.   old'person@ := person@
  105.   person@ := new'person@
  106.  
  107. Records can really help your programming, and on
  108. the Amiga they are absolutely necessary.
  109.  
  110. // by David Stidolph for AmigaCOMAL only
  111. RECORD type'person@ 
  112.   FIELD valid! 
  113.   FIELD last'name$ OF 25 
  114.   FIELD first'name$ OF 10 
  115.   FIELD inital$ OF 1 
  116.   FIELD sex$ OF 6 
  117.   FIELD married 
  118.   FIELD num'of'children 
  119.   FIELD phone$ OF 15 
  120. ENDRECORD type'person@ 
  121.  
  122. DIM person@ OF type'person@ 
  123. DIM choice$ OF 1, match$ OF 80 
  124. init'program 
  125. REPEAT  
  126.   menu(choice$) 
  127.   do'menu(choice$) 
  128. UNTIL choice$ IN "4qQ" 
  129. quit 
  130.  
  131. PROC init'program 
  132.   max'records:=20 
  133.   TRAP  
  134.     OPEN FILE 1,"demo.rnd",RANDOM
  135.       varsize(type'person@) //wrapline
  136.   HANDLER  
  137.     CREATE "demo.rnd",max'records,
  138.       varsize(type'person@) //wrapline
  139.     OPEN FILE 1,"demo.rnd",RANDOM
  140.       varsize(type'person@)//wrapline
  141.     // Amiga COMAL automatically initializes
  142.     // variables to null values, so we don't
  143.     // have to.
  144.     FOR record'num:=1 TO max'records DO  
  145.       WRITE FILE 1,record'num: person@ 
  146.     ENDFOR record'num 
  147.   ENDTRAP  
  148. ENDPROC init'program 
  149.  
  150. PROC menu(REF choice$) CLOSED 
  151.   DIM keystroke$ OF 1 
  152.   page 
  153.   PRINT "Main Menu" 
  154.   PRINT  
  155.   PRINT "1) Add person" 
  156.   PRINT "2) Delete person" 
  157.   PRINT "3) Find person" 
  158.   PRINT "4) Quit program" 
  159.   PRINT  
  160.   REPEAT  
  161.     PRINT "Your choice:"; 
  162.     choice$:=inkey$ 
  163.   UNTIL choice$ IN "1234adfqADFQ" 
  164.   PRINT  
  165.   PRINT  
  166. ENDPROC menu 
  167.  
  168. PROC do'menu(choice$) 
  169.   CASE choice$ OF  
  170.   WHEN "1","a","A" 
  171.     input'person(person@) 
  172.     record'num:=next'free 
  173.     IF record'num=0 THEN  
  174.       PRINT "No more records available!" 
  175.       press'return 
  176.     ELSE  
  177.       person@.valid!:=true 
  178.       WRITE FILE 1,record'num: person@ 
  179.     ENDIF  
  180.   WHEN "2","d","D" 
  181.     FOR record'num:=1 TO max'records DO  
  182.       READ FILE 1,record'num: person@ 
  183.       IF person@.valid! THEN  
  184.         display'person(person@) 
  185.         IF wants'deleted THEN  
  186.           person@.valid!:=false 
  187.           WRITE FILE 1,record'num: person@ 
  188.         ENDIF  
  189.       ENDIF  
  190.     ENDFOR record'num 
  191.   WHEN "3","f","F" 
  192.     INPUT "Enter characters to match: ": match$ 
  193.     FOR record'num:=1 TO max'records DO  
  194.       READ FILE 1,record'num: person@ 
  195.       IF person@.valid! THEN  
  196.         IF match$="*" OR match$ IN
  197.          combine$(person@) THEN//wrap line
  198.           display'person(person@) 
  199.           press'return 
  200.         ENDIF  
  201.       ENDIF  
  202.     ENDFOR record'num 
  203.   OTHERWISE
  204.     NULL // ignored
  205.   ENDCASE  
  206. ENDPROC do'menu 
  207.  
  208. PROC quit 
  209.   CLOSE FILE 2  
  210.   page 
  211.   END "Done." 
  212. ENDPROC quit 
  213.  
  214. PROC display'person(person@) 
  215.   page 
  216.   PRINT "Name:  ";person@.last'name$,","; 
  217.   PRINT person@.first'name$;person@.inital$ 
  218.   PRINT "Sex:   ";person@.sex$,","; 
  219.   IF person@.married THEN  
  220.     PRINT "is married"; 
  221.   ELSE  
  222.     PRINT "is not married"; 
  223.   ENDIF  
  224.   PRINT "and has";
  225.   PRINT person@.num'of'children;"children." 
  226.   PRINT "Can be reached at:";person@.phone$ 
  227.   PRINT  
  228. ENDPROC display'person 
  229.  
  230. FUNC combine$(person@) CLOSED 
  231.   RETURN person@.last'name$+person@.first'name$ 
  232. ENDFUNC combine$ 
  233.  
  234. PROC input'person(REF person@) 
  235.   INPUT "Last name: ": person@.last'name$ 
  236.   INPUT "First name: ": person@.first'name$ 
  237.   INPUT "Middle inital: ": person@.inital$ 
  238.   INPUT "Sex: ": person@.sex$ 
  239.   REPEAT  
  240.     INPUT "Married (y/n): ": confirm$ 
  241.   UNTIL confirm$ IN "yYnN" 
  242.   person@.married:=(confirm$ IN "yY")>0 
  243.   INPUT "Num of children: ":
  244.    person@.num'of'children //wrapline
  245.   INPUT "Phone number: ": person@.phone$ 
  246. ENDPROC input'person 
  247.  
  248. PROC press'return CLOSED 
  249.   PRINT  
  250.   PRINT "Press RETURN to continue" 
  251.   WHILE key$<>chr$(13) DO NULL 
  252. ENDPROC press'return 
  253.  
  254. FUNC next'free CLOSED 
  255.   IMPORT type'person@,max'records 
  256.   DIM person@ OF type'person@ 
  257.   FOR rec'num:=1 TO max'records DO  
  258.     READ FILE 1,rec'num: person@ 
  259.     IF NOT person@.valid! THEN RETURN rec'num 
  260.   ENDFOR rec'num 
  261.   RETURN 0 
  262. ENDFUNC next'free 
  263.  
  264. FUNC wants'deleted CLOSED 
  265.   DIM keystroke$ OF 1 
  266.   PRINT  
  267.   PRINT "Do you want to delete this entry? "; 
  268.   REPEAT  
  269.     keystroke$:=inkey$ 
  270.     IF NOT keystroke$ IN "yYnN" THEN  
  271.       PRINT chr$(7),chr$(25), 
  272.     ENDIF  
  273.   UNTIL keystroke$ IN "yYnN" 
  274.   RETURN keystroke$ IN "yY" 
  275. ENDFUNC wants'deleted
  276.  
  277.