The End of the Beginning (or vice versa?)
READ,DATA & RESTORE
This group of instructions can be extremely useful in certain programming
situations, for instance, where a large number of values are to be assigned to
variables before the main body of the program can be run. For example, in a program
to play music you might want to hold the frequencies of, say, 85 notes in an array.
After dimensioning the array, by the command DIM A(84), the values could be
assigned as follows:
20 A(0)=123:A(1)=125:A(2)=128. . . . . . .A(84)=222
It is much simpler, however, to put the values in DATA statements and then READ
them all into the array in one go:
10 DATA 123, 125, 128 . . . . . . . .
20 DATA . . . . . . . . 216,220,222
30 FOR J=0 TO 84: READ A(J): NEXT
Note that because the array contains a member A(0), you can always dimension it to
one less than the number of array members needed
When a BASIC program is run, the interpreter looks right through the program
and notes the position of all items included in the DATA statements. The DATA
statement pointer is then set to the beginning of the list. As each READ statement is
executed, the pointer is moved onto the next item. If at any time you want the
program to start at the beginning of the list, you just use the command RESTORE.
You may also want to READ from various positions down the list. As long as this
coincides with a line number you can use RESTORE X (where X is the relevant line
A very good example of this was in the Hangman program in the last issue of
Micropower, where the author needed first to refer to Numeric data string at line 8000
and then to String data starting at line 9000.
It is possible but not advisable to mix numeric and string items in DATA
10 DATA "Fred",5,"Jim",7,8,"Harry"
20 READ A$,X,B$,Y,Z,C$
However, if you make an error and try to read one type of data into the other type of
variable you will get a ‘SYN’ error – and serves you right !!