Pages 35–36 · Markdown

Chapter 5 – READ, DATA, RESTORE

READ, DATA and RESTORE

In some previous programs we saw that information, or data, can be entered directly into the computer using the INPUT statement. Sometimes this can be very tedious, especially if a lot of the data is repeated every time the program is run. You can save a lot of time by using the READ, DATA and RESTORE commands. For example:

10 READ a,b,c
20 PRINT a,b,c
30 DATA 10,20,30

A READ statement consists of READ followed by a list of the names of variables, separated by commas. It works rather like an INPUT statement, except that instead of getting you to type in the values to give to the variables, the computer looks up the values in the DATA statement.

Each DATA statement is a list of expressions – numeric or string expressions separated by commas. You can put them anywhere you like in a program, because the computer ignores them except when it is doing a READ. You must imagine the expressions from all the DATA statements in the program as being put together to form one long list of expressions, the DATA list. The first time the computer goes to READ a value, it takes the first expression from the DATA list; the next time, it takes the second; and thus as it meets successive READ statements, it works its way through the DATA list. (If it tries to go past the end of the DATA list, then it gives an error. See the next section for an easy way to avoid that)

Note that it's a waste of time putting DATA statements in a direct command, because READ will not find them. DATA statements have to go in the program. Let's see how these fit together in the program you've just typed in. Line 10 tells the computer to read three pieces of data and give them the variables a, b and c. Line 20 then says PRINT these variables. The DATA statement in line 30 gives the values of a, b and c. To see the order in which things work change line 20 to:

20 PRINT b,c,a

The information in DATA can be part of a FOR...NEXT loop. Type in:

10 FOR n=1 TO 6
20 READ d
30 DATA 2,4,6,8,10,12
40 PRINT d
50 NEXT n

When this program is RUN you can see the READ statement moving through the DATA list. DATA statements can also contain string variables. For example:

10 READ dat$
20 PRINT "The date is",dat$
30 DATA "January 1st, 2024"
40 STOP

This is the simple way of fetching expressions from the DATA list; start at the beginning and work through until you reach the end. However, you can make the computer jump about in the DATA list, using the RESTORE statement. This has RESTORE, followed by a line number, and makes subsequent READ statements start getting their data from the

first DATA statement at or after the given line number. (You can miss out the line number, in which case it is as though you had typed the line number of the first line in the program.)

Try this program:

10 READ a,b
20 PRINT a,b
30 RESTORE 10
40 READ x,y,z
50 PRINT x,y,z
60 DATA 1,2,3
70 STOP

In this program the data required by line 10 made a=1 and b=2. The RESTORE 10 instruction allowed x, y and z to be READ starting from the first number in the DATA statement. RUN this program again, without line 30 and see what happens.

Notes

You can store DATA statements in memory banks to take advantage of the expanded memory available on the ZX Spectrum Next. Refer to Chapter 23 – The Memory, for information on how to use BANK RESTORE to do this.

READ, DATA and RESTORE accept integer variables following the conventions set forth in Chapter 6 – Expressions.

DATA (function)

Apart from its use in subroutines as we saw in the previous chapter, DATA can also be used as a function that returns what the next DATA item to be read is; Return values can be: numeric (1), string (2) or none (0) – when there are no more data items available. This is most useful if we want to load a list of items with varying data types. Consider this example:

10 @rd:ON DATA:GO TO @prn:
   READ number: READ text$:
   ELSE GO TO @rd
20 GO TO @rd
30 @prn: PRINT text$;number
40 DATA 3,"Here we come KS"

What have we done here? We've used the handy ON...ELSE conditional selection structure and placed the DATA function in lieu of a variable argument since it returns one of 3 values. Then used the data type look ahead that DATA as a function provides to decide whether our next READ operation will be a numeric or string one (As ON expects its statements according to an increasing order of values, the exit condition is first).

Note that ELSE is really redundant here as there are 3 possible values of DATA and all are covered by the statements available, however it was put there as an additional demonstration of the complete ON...ELSE structure.


ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.