READ, DATA & RESTORE
If you wish to use data from within a program rather than input values from a keyboard, which for a long list may tend to be very tedious, we can use the READ-DATA statements. For example, suppose we wish to incorporate a list of prices of several items in a program, we can utilize READ-DATA statements as follows:
100 READ Hammercost,Nailcost,Nailcost,Drivercost,Screwcost
110 data 2.50,0.01,1.75.0.02
The values of the variables Hamercost,Nailcost,Drivercost,Screwcost in the READ statement 100 are assigned the values in the exact corresponding order as given in the DATA statement of 110. So after execution of Line 100, data would have been taken from the DATA statement and assigned as follows:
Hamercost = 2.50,Nailcost = 0.01,Drivercost =1.75,Screwcost = 0.02
Each READ statement in a program consists of READ followed by a list of variable identifiers, each identifier being separated from the next by a comma. Each DATA statement is a list of values and/or expressions, each value mey be put anywhere you wish in a program – normally at the beginning or at the end if this is convenient – since the computer ignores any DATA statements until it meets a READ statement.
The first time the computer, in executing the program meets a READ statement it takes for the first variable value variable and the first data value from the DATA list, for the second variable and the second data value, and so on, working its way through the whole of the DATA statement lists.
Note: In order to use READ successfully on the QL, before it is used for the first time in the program, you must use the RESTORE command so that the READ and DATA statements are aligned.
If the number of items in the READ and DATA statements do not match, e.g. if there are more variables in the READ statements that values in the DATA statements, then the computer will display an error message immediately as it tries to READ a vale which is not there.
1 This simple program illustrates the basic function of READ-DATA statements.
5 CLS: RESTORE
10 READ A,B,C
20 PRINT"FIRST ITEM A= ";A
30 PRINT"SECOND ITEM B=";B
40 PRINT"THIRD ITEM C=" ;C
50 DATA 111,222,333
RUN<ENTER>
FIRST ITEM A= 111
SECOND ITEM B=222
THIRD ITEM C=333
..the display obtained on screen showing that A is assigned the first value in the DATA statement list, B the second and C the third.
Note: Line 5 contains two instructions, CLS to clear the screen, and the RESTORE command to ‘restore” or bring the data pointer to the beginning of the DATA list, so that it definitely points at the first value.
2 This program works out the average value of a number of items listed in READ-DATA statements.
5 CLS: RESTORE
10 REMark ** To find the average of a list of values **
20 READ A,B,C,D,E,F,G
30 READ H,I,J,K,L,M,N
40 SUM= A+B+C+D+E+F+G
50 SUM= SUM+H+I+J+K+L+M+N
60 PRINT "AVERAGE=";SUM/14
70 DATA 14,23,32,47,56,66,72,83
80 DATA 93,104,116,127,138,147
RUN <ENTER>
AVERAGE=79.85714...Display of results obtained
3 This program obtains DATA using READ-DATA statements and works out sub-total costs and total costs for tools and materials purchased:
30 hammers at $2.75 each
2,800 nails at $0.005 each
14 screwdrivers at $1.75 each
1,585 screws at $0.01 each
20 sheets of hardboard at $12.25 each
5 CLS:RESTORE
10 REMark ** COST PROGRAM **
20 READ No_HAM,HAM_Cost
30 T_H_Cost=No_HAM*HAM_Cost
40 READ No_Nail,Nail_Cost
50 T_N_Cost=No_Nail*Nail_Cost
60 READ No_Driv,Driv_Cost
70 T_D_Cost=No_Driv*Driv_Cost
80 READ No_Screw,Screw_Cost
90 T_S_Cost=No_Screw*Screw_Cost
100 READ NO_Hardb,Hardb_Cost
110 T_HB_Cost=NO_Hardb*Hardb_Cost
120 Total_Cost=T_H_Cost+T_N_Cost+T_D_Cost
130 Total_Cost=Total_Cost+T_N_Cost+T_HB_Cost
140 PRINT"Total Hammer Cost =";T_H_Cost
150 PRINT"Total Nail Cost=";T_N_Cost
160 PRINT"Total Screw-Dr.Cost=";T_D_Cost
170 PRINT"Total Screw Cost=";T_S_Cost
180 PRINT"Total Hardbd Cost=";T_HB_Cost
190 PRINT"- - - - - - - - - - - - - - -"
200 PRINT"Total Cost=";Total_Cost
210 DATA 30,2.75,2800,5E-3,14,1.75
220 DATA 1585,1E-2,20,12.25
RUN <ENTER>
Total Hammer Cost = 82.50
Total Nail Cost = 14.00
Total Screw-Dr Cost= 24.50
Total Screw Cost = 15.85
Total HardBd Cost =245.00
________________________________
Total Cost =381.85 ... display obtained
Note: The total will NOT be 381.85 because the QL changed two elements in both DATA lines, with exponent numbers. Total is 380.
Frequently we may wish to STOP program execution at a given line and make a check on the results so far obtained before allowing the execution to continue. This is especially useful in checking intermediate results in a lengthy program.
For STOPing execution of a program at a given line, SuperBASIC provides the STOP statement:
100 STOP
This statement STOPs execution of the program at line 100 and will return SuperBASIC to the command mode.
The CONTINUE command allows a program which has been interrupted by a STOP statement (or by a BREAK action) to be resumed at the next line after the STOP statement. (To BREAK program execution , hold down the CTRL key and press the space bar. This will return the cursor to the screen.) For example, consider the program:
5 CLS: RESTORE
10 DATA 70,450,12.3,67,90,23.6
20 DATA 34.8,923,65.4,768.9
30 READ A,B
40 PRINT (A-32)*5/9,B*4.2
50 STOP
60 READ C,D,E
70 PRINT (C+D)*A-E
80 STOP
RUN <ENTER>
21.111111 1890
CONTINUE <ENTER>
5461
On running the program, execution starts from the beginning, READs in DATA for A,B at line 30, outputs results at line 40 and then is stopped by the STOP statement at line 50. Program execution can be restarted from line 60 by typing in the direct mode command CONTINUE. A further READ is made at line 60, results output at line 70 and then execution stopped at line 80 by a second STOP statement.
In ‘normal’ use of READ-DATA statements, the computer works from the beginning of the first DATA statement (the one with the lowest line number) when it meets the first READ statement and then subsequently works works through all the DATA lists until it reaches the end.
This sequence can be ‘restored’ by using RESTORE statements which have the form:
100 RESTORE
100 RESTORE (line number)
The first RESTORE statement (with no line numbers after RESTORE) forces any subsequent READ statement to start reading data from the first value of the first DATA statement (lowest number) in the program. When a specified line number is given after RESTORE, then any subsequent READ statement takes its data from the first DATA statement at or immediately following the specified line number in the RESTORE statement.
For example, let us work through the following program:
5 CLS:RESTORE
10 READ A,B,C
20 PRINT A,B,C
30 RESTORE
40 READ X,Y,Z
50 PRINT X,Y,Z
60 READ A,B,C
70 PRINT A,B,C
80 DATA 1,2,3,4,5,6
RUN <ENTER>
1 2 3 ...(display of A,B,C at line 20)
1 2 3 ...(display of X,Y,Z at line 50)
4 5 6 ...(display of A,B,C at line 70)
At line 10 the READ statement assigns A=1, B=2 and C=3, i.e. the first three values in the DATA list of line 80. Thus at line 20 we obtain the display:
1 2 3
Line 30, the RESTORE statement, returns us to the beginning of the DATA list. Thus at line 40, the READ statement assigns X=1, Y=2, Z=3 and line 50 produces display (again):
1 2 3
AT line 60, however, the READ statement ‘pointer’ is at the fourth item in the DATA list and hence this READ statement assigns A=4, B=5, C=6 and this is confirmed by the action of the display statement 70:
4 5 6
Finally, consider a second example:
5 CLS:RESTORE
10 DATA 1,2,3,4,5,6
20 DATA 10,20,30,40
30 DATA 100,200,300
40 READ A,B,C
50 PRINT A,B,C,(A+B+C)/3
60 RESTORE 20
70 READ A,B,C,D
80 PRINT A,B,C,D,(A+B+C+D)/4
90 RESTORE 10
100 READ A,B,C,D,E
110 READ F,G,H,I,J,K,L,M
120 PRINT A;B;C;D;E;F;G;H;I;J;K;L;M
RUN <ENTER>
1 2 3 2
10 20 30 40 25
1234561020 30 40 100 200 300
is the display obtained.
At line 60 the RESTORE statement restores the READ pointer to the first value in DATA statement 20. Thus the READ statement at line 70 assigns A=10, B=20, C=30, D=40. In the absence of 60 RESTORE 20, DATA would have been taken from the next value in line 10, i.e. A=4, B=5, C=6 and then from line 20, D=10.
AT line 90, the RESTORE 10 statement returns the READ pointer to the beginning of DATA list from the first value in 10 and continuing with 120 to work right through the complete DATA statements 10, 20 and 30.
The next listing is a simple telephone directory using READ, RESTORE and DATA statements.
5 REMark LIST friends
10 REMark Data statement example
15 CLS
100 DIM name$(10,28)
110 RESTORE
120 :
130 FOR count = 1 TO 7: READ name$(count)
140 PRINT name$
150 DATA "Bob Malloy [phone number omitted] ", "Tom Skapinsky [phone number omitted]", "Fred Stern [phone number omitted]", "John Pazmino [phone number omitted]", "George Gilder [phone number omitted]", "Robert Gilder [phone number omitted]", "Joe LaPunzina [phone number omitted]"
The output:
Bob Malloy [phone number omitted] Tom Skapinsky [phone number omitted] Fred Stern [phone number omitted] John Pazmino [phone number omitted] George Gilder [phone number omitted] Robert Gilder [phone number omitted] Joe LaPunzina [phone number omitted]
In order to make the output display uniform, spaces were padded between the Names and Telephone numbers.
5 CLS
7 RESTORE
10 REMark ** Exchange Rates **
20 DIM country$(10,15)
30 DIM EX_RATE(10)
40 FOR j=1 TO 10
50 READ country$(j)
60 PRINT"Enter Exchange Rate"
70 PRINT "`1 for ";country$(j); "=";
75 INPUT EX_RATE(j)
80 END FOR j
90 PRINT "List of exchange rates"
100 FOR j=1 TO 10
110 PRINT country$(j),EX_RATE(j)
120 END FOR j
130 DATA "Austria ","Belgium ","Denmark ","France "
140 DATA "W.Germany","Greece ","Italy ","Holland "
150 DATA "Switzerland","U.S.A. "
Exchange Rates utilizes both string and numeric variable arrays. The string information – names of countries – is held in DATA statements. The input statement in conjunction with the FOR statement is used to ENTER in numerical data – exchange rates, in this example. The final part of the program displays the countries and the corresponding exchange rate. See you next month… Bob Gilder
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.