home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / siena / oplexam / B05DATA.TXT < prev    next >
Encoding:
Text File  |  1994-09-06  |  18.6 KB  |  572 lines

  1.  
  2.  
  3.  
  4. Data file handling
  5.  
  6. You can use OPL to create data files (databases) like 
  7. those used by the Database. You can store any kind of information 
  8. in a data file, and retrieve it for display, editing or calculations.
  9.  
  10. This chapter covers:
  11.  
  12. Creating data files
  13.  
  14. Adding and editing records
  15.  
  16. Searching records
  17.  
  18. Using a data file both in OPL and in the Database
  19.  
  20.  
  21.  
  22.  
  23. ****** Files, records and fields
  24.  
  25. Data files (or databases)
  26.     (picture)
  27.  
  28. are made up of records
  29.  
  30.  
  31.     (picture)
  32.  
  33. For example, in a data file of names and addresses, each record might 
  34. have a name field, a telephone number field, and separate fields for 
  35. each line of the address.
  36.  
  37. In OPL you can:
  38.  
  39. *) Create a new file with CREATE, or open an 
  40. existing file with OPEN, and copy, delete and rename files 
  41. with COPY, DELETE and RENAME.
  42.  
  43. *) Add a new record with APPEND, change an existing 
  44. one with UPDATE, and remove a record with ERASE.
  45.  
  46. *) Fill in a field by assigning a value to a field variable.
  47.  
  48.  
  49.  
  50.  
  51. ****** Creating a data file
  52.  
  53. Use the CREATE command like this:
  54.  
  55. CREATE filename$,logical name,field1,field2,...
  56.  
  57. For example:
  58.  
  59. CREATE "clients",B,nm$,tel$,ad1$,ad2$,ad3$
  60.  
  61. creates a data file called "clients".
  62.  
  63. The file name is a string, so remember to put quote marks around it. 
  64. You can also assign the name string to a string variable (for example 
  65. "fil$="clients"") and then use the variable name as the argument  
  66. "CREATE fil$,A,field1,field2".
  67.  
  68. *** Logical names
  69.  
  70. You can have up to 4 data files open at a time. Each of these must 
  71. have a logical name: "A", "B", "C" or "D". The logical 
  72. name lets you refer to this file without having to keep using the 
  73. full file name. 
  74.  
  75. A different logical name must be used for each data file opened  
  76. one called "A", one called "B", one called "C" and one 
  77. called "D". A file does not have to be opened with the same logical 
  78. name as the last time it was opened. When a file is closed, its logical 
  79. name is freed for use by another file.
  80.  
  81. *** Fields
  82.  
  83. "field1, field2",... are the field names  up to 32 in any 
  84. record. These are like variables, so  use "%" "&" or "$" 
  85. to make the appropriate types of fields for your data. You cannot 
  86. use arrays. Do not specify the maximum length of strings that the 
  87. string fields can handle. The length is automatically set at 255 characters.
  88.  
  89. Field names may be up to 8 characters long, including any qualifier 
  90. like "&".
  91.  
  92. When referring to fields, add the logical file name to the front of 
  93. the field name, to specify which opened file the fields belong to. 
  94. Separate the two by a dot. For example, "A.name$" is the "name$" 
  95. field of the file with logical name "A", and "C.age%" is the 
  96. "age%" field of the file with logical name "C".
  97.  
  98. The values of all the fields are 0 or null to start with. You can 
  99. see this if you run this example program: 
  100.  
  101. PROC creatfil:
  102.   CREATE "example",A,int%,long&,float,str$
  103.   PRINT "integer=";a.int%
  104.   PRINT "long=";a.long&
  105.   PRINT "float=";a.float
  106.   PRINT "string=";a.str$
  107.   CLOSE
  108.   GET
  109. ENDP
  110.  
  111.  
  112. ****** Opening a file
  113.  
  114. When you first CREATE a data file it is automatically open, 
  115. but it closes again when the program ends. If 
  116. a file already exists, trying to CREATE it again will give 
  117. an error- so if you ran the procedure "creatfil:" a second 
  118. time you would get an error. To open an existing file, use the OPEN 
  119. command.
  120.  
  121. OPEN works in the same way as the CREATE command. For 
  122. example:
  123.  
  124. OPEN "clients",B,a$,b$,c$,d$,e$
  125.  
  126. *) You must use the same filename as when you first created 
  127. it.
  128.  
  129. *) You must include in the OPEN command each of the 
  130. fields you intend to alter or read. You can omit fields from the end 
  131. of the list; you cannot miss one out from the middle of the 
  132. list, for example "field1$,,name$" They must remain the same type 
  133. of field, but you can change their names. So a file created with fields 
  134. "name$,age%" could later be opened with the fields "a$,x%".
  135.  
  136. *) Give the file a logical name. Up to 4 files may be open 
  137. at any one time, with logical names "A", "B", "C" and 
  138. "D". You can't have two files open simultaneously with the same 
  139. logical name, so when opening the files, remember which logical names 
  140. you have already used.
  141.  
  142. You might make a new module, and type these two procedures into 
  143. it: 
  144.  
  145. PROC openfile:
  146.   IF NOT EXIST("example")
  147.     CREATE "example",A,int%,lng&,fp,str$
  148.   ELSE
  149.     OPEN "example",A,int%,lng&,fp,str$
  150.   ENDIF
  151.   PRINT "Current values:"
  152.   show:
  153.   PRINT "Assigning values"
  154.   A.int%=1
  155.   A.lng&=&2**20   REM the 1st & avoids integer overflow
  156.   A.fp=SIN(PI/6)
  157.   PRINT "Give a value for the string:"
  158.   INPUT A.str$
  159.   PRINT "New values:"
  160.   show:
  161. ENDP
  162.  
  163.  
  164. PROC show:
  165.   PRINT "integer=";A.int%
  166.   PRINT "long=";A.lng&
  167.   PRINT "float=";A.fp
  168.   PRINT "string=";A.str$
  169.   GET
  170. ENDP
  171.  
  172. *** Notes
  173.  
  174. Opening/creating the file
  175. The IF...ENDIF checks to see if the file already exists, using 
  176. the EXIST function. If it does, the file is opened; if it doesn't, 
  177. the file is created.
  178.  
  179. Giving values to the fields
  180. The fields can be assigned values just like variables. The field name 
  181. must be used with the logical file name like this: "A.f%=1" 
  182. or "INPUT A.f$". 
  183.  
  184. If you try to give the wrong type of value to a field (for example 
  185. ""Davis"" to "f%") an error message will be displayed.
  186.  
  187. You can access the fields from other procedures, just like global 
  188. variables. Here the called procedure "show:" displays the values 
  189. of the fields.
  190.  
  191. Field names
  192. You must know the type of each field, and you must give each a separate 
  193. name  you cannot refer to the fields in any indexed way, eg 
  194. as an array.
  195.  
  196. Opening a file for sharing
  197. The OPENR command works in exactly the same way as OPEN, 
  198. except that the file cannot be written to (with UPDATE or APPEND), 
  199. only read. However, more than one running program can then look at 
  200. the file at the same time.
  201.  
  202.  
  203. ****** Saving records
  204.  
  205. The last example procedure did not actually save the field values 
  206. as a record to a file. To do this you need to use the APPEND 
  207. command. This program, for example, allows you to add records to the 
  208. "example" data file:
  209.  
  210. PROC count:
  211.   LOCAL reply%
  212.   OPEN "example",A,f%,f&,f,f$
  213.   DO
  214.     CLS
  215.     AT 20,1 :PRINT "Record count=";COUNT
  216.     AT 9,5 :PRINT "(A)dd a record"
  217.     AT 9,7 :PRINT "(Q)uit"
  218.     reply%=GET
  219.     IF reply%=%q OR reply%=%Q
  220.       BREAK
  221.     ELSEIF reply%=%A OR reply%=%a
  222.       add:
  223.     ELSE
  224.       BEEP 16,250
  225.     ENDIF
  226.   UNTIL 0
  227. ENDP
  228.  
  229. PROC add:
  230.   CLS
  231.   PRINT "Enter integer field:";
  232.   INPUT A.f%
  233.   PRINT "Enter long integer field:";
  234.   INPUT A.f&
  235.   PRINT "Enter numeric field:";
  236.   INPUT A.f
  237.   PRINT "Enter string field:";
  238.   INPUT A.f$
  239.   APPEND
  240. ENDP
  241.  
  242. BEEP
  243. The BEEP command makes a beep of varying pitch and length:
  244.  
  245. BEEP duration%,pitch%
  246.  
  247. The duration is measured in  1/32 s of a second, so "duration%=32" 
  248. would give a beep a second long. Try "pitch%=50" for a high beep, 
  249. or "500" for a low beep.
  250.  
  251. *** The number of records
  252.  
  253. The COUNT function returns the number of records in the file. 
  254. If you use it just after creating a database, it will return 0. As 
  255. you add records the count increases.
  256.  
  257. *** How the values are saved
  258.  
  259. Use the APPEND command to save a new record. This has no arguments. 
  260. The values assigned to "A.f%", "A.f&", "A.f" and "A.f$" 
  261. are added as a new record to the end of the "example" data file. 
  262. If you only give values to some of the fields, not all, you won't 
  263. see any error message. If the fields happen to have values, these 
  264. will be used; otherwise  null strings ("") will be given to string 
  265. fields, and zero to numeric fields.
  266.  
  267. New field values are always added to the end of the current data 
  268. file  as the last record in the file (if the file is a new 
  269. one, it will also be the first record).
  270.  
  271. At any time while a data file is open, the field names currently in 
  272. use can be used like any other variable  for example, in a 
  273. PRINT statement, or a string or numeric expression.
  274.  
  275. APPEND and UPDATE 
  276. APPEND adds the current field values to the end of the file 
  277. as a new record, whereas UPDATE deletes the current record 
  278. and adds the current field values to the end of the file as a new 
  279. record.
  280.  
  281.  
  282. ****** Moving from record to record
  283.  
  284. When you open or create a file, the first record in the file is current. 
  285. To read, edit, or erase another record, you must make that record 
  286. current  that is, move to it. Only one record is current at 
  287. a time. To change the current record, use one of these commands: 
  288.  
  289. POSITION `moves to' a particular record, setting the field 
  290. variables to the values in that record. For example, the instruction 
  291. "POSITION 3" makes record 3 the current record. The first record 
  292. is record 1.
  293.  
  294. You can find the current record number by using the POS function, 
  295. which returns the number of the current record.
  296.  
  297. FIRST moves to the first record in a file.
  298.  
  299. NEXT moves to the following record in a file. If the end of 
  300. the file is passed, NEXT does not report an error, but the 
  301. current record is a new, empty record. This case can be tested for 
  302. with the EOF function.
  303.  
  304. BACK moves to the previous record in the file. If the current 
  305. record is the first record in the file then that first record stays 
  306. current. 
  307.  
  308. LAST moves to the last record in the file.
  309.  
  310. *** Deleting a record
  311.  
  312. ERASE deletes the current record in the current 
  313. file.
  314.  
  315. The next record is then current. If the erased record was the last 
  316. record in a file, then following this command the current record will 
  317. be empty and EOF will return true.
  318.  
  319.  
  320. ****** Finding a record 
  321.  
  322. FIND makes current the next record which has a field matching 
  323. your search string. Capitals and lower-case letters match. For example:
  324.  
  325. r%=FIND("Brown")
  326.  
  327. would select the first record containing a string field with the value 
  328. "Brown", "brown" or "BROWN", etc. The number of that record is returned, 
  329. in this case to the variable "r%". If the number returned is zero, 
  330. no matching field was found. Any other number means that a match was 
  331. found.
  332.  
  333. The search includes the current record. So after finding a matching 
  334. record, you need to use NEXT before you can continue searching 
  335. through the following records. 
  336.  
  337. "FIND("Brown")" would not find a field "Mr Brown". To find this, 
  338. use wildcards, as explained below.
  339.  
  340. You can only search string fields, not number fields. For example, 
  341. if you assigned the value 71 to the field "a%", you could not 
  342. find this with FIND. But if you assigned the value "71" to 
  343. "a$", you could find this.
  344.  
  345. *** Wildcards
  346.  
  347. "r%=FIND("*Brown*")" would make current the next record containing 
  348. a string field in which "Brown" occurred  for example, 
  349. the fields "MR BROWN", "Brown A.R." and "Browns Plumbing" 
  350. would be matched. The wildcards you can use are:
  351.  
  352. ?      matches any one character
  353.  
  354. *       matches any number of characters.
  355.  
  356. Once you've found a matching record, you might display it on the screen, 
  357. erase it or edit it. For example, to display all the records containing 
  358. "BROWN":
  359.  
  360. FIRST
  361. WHILE FIND("*BROWN*")
  362.   PRINT a.name$, a.phone$
  363.   NEXT
  364.   GET
  365. ENDWH
  366.  
  367. *** More controlled finding
  368.  
  369. FINDFIELD, like FIND, finds a string, makes the record 
  370. with this string the current record, and returns the number of this 
  371. record. However you can also use it to do case-dependent searching, 
  372. to search backwards through the file, to search from the first record 
  373. (forwards) or from the last record (backwards), and to search in one 
  374. or more fields only.
  375.  
  376. You may experience some problems in using FINDFIELD with some 
  377. versions of OPL. To ensure that problems are avoided use the line:
  378.  
  379. POKEB(peekw($1c)+7),0
  380.  
  381. immediately before each call to FINDFIELD.
  382.  
  383. The first argument to FINDFIELD is the string to look for, 
  384. as for FIND. The second is the number of the field to start 
  385. looking in (1 for the first field), and the third is the number of 
  386. fields to search in (starting from the field specified by the second 
  387. argument). If you want to search in all fields, use 1 as the second 
  388. argument and for the third argument use the number of fields you used 
  389. in the OPEN/CREATE command.
  390.  
  391. The fourth argument adds together two values:
  392.  
  393. *) 0 for a case independent match, where capitals and 
  394. lower-case letters match, or 1 for a case dependent match. This 
  395. value should be multiplied by 16.
  396.  
  397. *) 0 to search backwards from the current record, 1 to search 
  398. forwards from the current record, 2 to search backwards from the end 
  399. of the file, or 3 to search forwards from the start of the file.
  400.  
  401. For a case independent search (0) forwards from the current record 
  402. (1*16), use 0+(1*16), ie 16. If you wanted to search only in the second 
  403. and third fields, the full statement might look like this: "FINDFIELD("brown",2,2,16)".
  404.  
  405. If you understand hexadecimal arithmetic, as described 
  406. under HEX$ in the `Alphabetic listing' chapter, note that you 
  407. can combine the two values more easily using hexadecimal numbers. 
  408. Use a "$" symbol, then the digit for the start/direction (0/1/2/3), 
  409. then the digit for case dependency (0/1). In the previous example 
  410. the digits are 1 and 0, producing the hexadecimal number "$10" 
  411. (=16).
  412.  
  413. If you find a matching record and then want to search again from this 
  414. record, you must first use NEXT or BACK (according to 
  415. the direction you are searching in), otherwise the same match will 
  416. be "found" in the current record again."
  417.  
  418.  
  419. ****** Changing/closing the current file
  420.  
  421. Immediately after a file has been created or opened, it is automatically 
  422. current. This means that the APPEND or UPDATE commands 
  423. save records to this file, and the record-position commands (explained 
  424. below) move around this file. You can still use the fields of other 
  425. open files, for example "A.field1=B.field2"
  426.  
  427. USE makes current one of the other opened files. For example 
  428. "USE B" selects the file with the logical name B (as specified 
  429. in the OPEN or CREATE command which opened it).
  430.  
  431. If you attempt to USE a file which has not yet been opened 
  432. or created, an error is reported. 
  433.  
  434. In this procedure, the EOF function checks whether you are 
  435. at the end of the current data file  that is, whether you've 
  436. gone past the last record. You can use EOF in the test 
  437. condition of a loop  UNTIL EOF or WHILE NOT EOF 
  438. in order to carry out a set of actions on all the records in a file.
  439.  
  440. ***  Example  copies selected records from one file to another
  441.  
  442. PROC copyrec:
  443.   OPEN "example",A,f%,f&,f,f$
  444.   TRAP DELETE "temp"
  445.   REM If file doesn't exist, ignore error
  446.   CREATE "temp",B,f%,f&,f,f$
  447.   PRINT "Copying EXAMPLE to TEMP"
  448.   USE A REM the EXAMPLE file
  449.   DO
  450.     IF a.f%>30 and a.f<3.1415
  451.       b.f%=a.f%
  452.       b.f&=a.f&
  453.       b.f=a.f
  454.       b.f$="Selective copy"
  455.       USE B REM the TEMP file
  456.       APPEND
  457.       USE A
  458.     ENDIF
  459.     NEXT
  460.   UNTIL EOF REM until End Of File
  461.   CLOSE REM closes A; B becomes current
  462.   CLOSE REM closes B
  463. ENDP
  464.  
  465. This example uses the DELETE command to delete any "temp" 
  466. file whch may exist, before making it afresh. Normally, if there was 
  467. no "temp" file and you tried to delete it, an error would be generated. 
  468. However, this example uses TRAP with the DELETE command. 
  469. TRAP followed by a command means "if an error occurs in the 
  470. command, carry on regardless".
  471.  
  472. There are more details of TRAP in the chapter on `Error Handling'.
  473.  
  474. *** Closing a data file
  475.  
  476. You should always `close' a data file (with the CLOSE command) 
  477. when you have finished using it. Data files close automatically when 
  478. programs end. You can only have 4 files open at a time  if 
  479. you have 4 files open and you want to access another one, close one 
  480. of them. CLOSE closes the current file.
  481.  
  482. *** Keeping data files compressed
  483.  
  484. When you change or delete records in a data file, the space taken 
  485. by the old information is not automatically recovered. By default, 
  486. the space is recovered when you close the file, provided it 
  487. is on `Internal drive' or on a RAM SSD (ie it is not on a Flash 
  488. SSD).
  489.  
  490. Closing a very large file which contains changed or deleted records 
  491. can be slow when compression is enabled, as the whole file beyond 
  492. each old record needs copying down, each time.
  493.  
  494. You can prevent data file compression if you wish, with these 
  495. two lines:
  496.  
  497. p%=PEEKW($1c)+$1e
  498. POKEW p%,PEEKW(p%) or 1
  499.  
  500. (Use any suitable integer variable for "p%".) Files used by the 
  501. current program will now not compress when they close.
  502.  
  503. Use these two lines to re-enable auto-compression:
  504.  
  505. p%=PEEKW($1c)+$1e
  506. POKEW p%,PEEKW(p%) and $fffe
  507.  
  508. Warning: be careful to enter these lines exactly as shown. These 
  509. examples work by setting a system configuration flag.
  510.  
  511. If you have closed a file without compression, you can recover 
  512. the space by using the COMPRESS command to create a new, compressed 
  513. version of the file. "COMPRESS "dat" "new"", for example, 
  514. creates a file called "new" which is a compressed version of "dat", 
  515. with the space which was taken up by old information now recovered. 
  516. (You have to use COMPRESS to compress data files which are 
  517. kept on a Flash SSD.)
  518.  
  519.  
  520. ****** Data files and the Database
  521.  
  522. The files you use with the Database (listed under the Data icon in 
  523. the System screen)  often called databases or database 
  524. files  are also just data files.
  525.  
  526. Data files created by the Database can be viewed in OPL, and vice 
  527. versa.
  528.  
  529. In OPL: to open a data file made by the Database, begin its 
  530. name with "\DAT\", and end it with ".DBF". For example, 
  531. to open the file called "data" which the Database normally uses:
  532.  
  533. OPEN "\dat\data.dbf",A,a$,b$,c$,d$...
  534.  
  535. Restrictions:
  536.  
  537. *) You can use up to 32 field variables, all strings. It is 
  538. possible for records to contain more than 32 fields, but these fields 
  539. cannot be accessed by OPL. It's safe to change such a record and use 
  540. UPDATE, though, as the extra fields will remain unchanged.
  541.  
  542. *) The maximum record length in OPL is 1022 characters. You 
  543. will see a `Record too large' error (-43) if your program tries 
  544. to open a file which contains a record longer than this.
  545.  
  546. *) The Database breaks up long records (over 255 characters) 
  547. when storing them. They would appear as separate records to OPL.
  548.  
  549. In the Database: to examine an OPL data file, press the Data 
  550. button, select `Open' from the `File' menu, and type the name with 
  551. "\OPD\" on the front and ".ODB" at the end  for example:
  552.  
  553.   \opd\example.odb
  554.  
  555. Restrictions:
  556.  
  557. *) All of the fields must be string fields.
  558.  
  559. *) You can have up to a maximum of 32 fields, as specified 
  560. in the CREATE command. If you view an OPL data file with the 
  561. Database, and add more lines to records than the number of fields 
  562. specified in the original CREATE command, you will get an error 
  563. if you subsequently try to access these additional fields in OPL.
  564.  
  565. In both cases, you are using a more complete file specification. 
  566. There is more about file specifications in the Advanced Topics chapter.
  567.  
  568.  
  569.  
  570.  
  571.  
  572.