Contents / Part 3 / Previous chapter / Next chapter / Index


22 READ, DATA, RESTORE

One very common way of storing a whole set of information along with the computer program is to use DATA statements. You will remember that computer programs can be stored on cassette and sets of data can be stored in the program as well. For example it might be necessary in a program to convert the month given as a number into a name. The program below stores the names of the month as DATA

5 REPEAT

10 PRINT "GIVE THE MONTH AS A NUMBER"

20 INPUT M

30 UNTIL M>0 AND M<13

40 FOR X=1 TO M

50 READ A$

60 NEXT X

70 PRINT "THE MONTH IS ";A$

100 DATA JANUARY, FEBRUARY, MARCH, APRIL

110 DATA MAY, JUNE, JULY, AUGUST, SEPTEMBER

120 DATA OCTOBER, NOVEMBER, DECEMBER

>RUN

GIVE THE MONTH AS A NUMBER

?6

THE MONTH IS JUNE

Lines 10 to 30 repeat until a sensible value (or M is entered) - it must be between 1 and 12. In the example run a value of 6 was given to M. In this case the FOR...NEXT loop between lines 40 and 60 will go round 6 times. Each time it goes around it READs the next piece of DATA into A$ until finally A$ will be

left containing JUNE. It might make it clearer if an extra line is temporarily inserted at line 55 to print out the value of A$ and X each time around the loop.

>55PRINT A$,X

>

>LIST

5 REPEAT

10 PRINT "GIVE THE MONTH AS A NUMBER"

20 INPUT M

30 UNTIL M>0 AND N<13

40 FOR X=1 TO M

50 READ AS

55 PRINT A$,X

60 NEXT X

70 PRINT "THE MONTH IS ";A$

100 DATA JANUARY, FEBRUARY, MARCH, APRIL

110 DATA MAY, JUNE, JULY, AUGUST, SEPTEMBER

120 DATA OCTOBER, NOVEMBER, DECEMBER

>

>RUN

GIVE THE MONTH AS A NUMBER

?6

JANUARY 1

FEBRUARY 2

MARCH 3

APRIL 4

MAY 5

JUNE 6

THE MONTH IS JUNE

This is quite a neat way of getting to the (say) sixth element of a list but there is another way using an array.

Sometimes there is more than one set of data and it is useful to be able to set the 'data pointer' to a selected set of data. The next program has two sets of data each containing a set of prices and car names. One set of data refers to British Leyland cars and the other to Lotus cars.

10 REPEAT

20 PRINT "DO YOU PREFER BL OR LOTUS CARS? ";

30 A$=GET$

40 PRINT A$

50 IF A$="B" THEN RESTORE 170 ELSE RESTORE 340

60 INPUT "HOW MANY POUNDS CAN YOU AFFORD ",P

80 PRINT "YOU CAN AFFORD THESE THEN:"

90 FOR X=1 TO 15

100 READ NAME$

110 READ PRICE

120 IF PRICE <P THEN PRINT PRICE,TAB(15);NAME$

130 NEXT X

140 PRINT

150 UNTIL FALSE

160

170 REM BRITISH LEYLAND CARS

180 DATA MINI 1000 CITY, 2898

190 DATA METRO HLE, 4198

200 DATA DOLOMITE 1300,4351

210 DATA SPITFIRE 1500, 4696

220 DATA TRIUMPH ACCLAIM HLS, 4988

230 DATA ALLEGRO 31.3 HLS, 5095

240 DATA DOLOMITE 1500HL, 5225

250 DATA PRINCESS 2 HL, 5899

260 DATA DOLOMITE SPRINT, 7118

270 DATA TR7 FIXEDHEAD, 7258

280 DATA ROVER 2300, 7450

290 DATA DAIMLER SOVEREIGN 4.2, 16259

300 DATA JAGUAR XJ6 4.2,16279

310 DATA DAIMLER VANDEN PLAS 4.2, 21419

320 DATA DAIMLER LIMOUSINE, 26998

330

340 REN LOTUS CARS

350 DATA ESPRIT S3, 13513

360 DATA ECLAT SERIES 2.2, 14857

370 DATA TURBO ESPRIT, 16982

380 RATA ELITE SERIES 2.2, 17206

>RUN

DO YOU PREFER BL OR LOTUS CARS? B

HOW MANY POUNDS CAN YOU AFFORD ?10000

YOU CAN AFFORD THESE THEN:

2898 MINI 1000 CITY

4198 METRO HLE

4351 DOLOMITE 1300

4696 SPITFIRE 1500

4988 TRIUMPH ACCLAIM HLS

5095 ALLEGRO 31.5 HLS

5225 DOLOMITE 1500HL

5899 PRINCESS 2 HL

7118 DOLOMITE SPRINT

7258 TR7 FIXEDHEAD

7450 ROVER 2300

DO YOU PREFER BL OR LOTUS CARS? L

HOW MANY POUNDS CAN YOU AFFORD ?1700

YOU CAN AFFORD THESE THEN:

Out of DATA at Line 100

You will notice that line 50 uses the RESTORE statement to set the data 'pointer' to either line 170 where BL, data is stored or to line 340 where Lotus data is stored. This ensures that data is read from the correct list.

Lines 90 to 130 attempt to read off 15 sets of data from the data lists but fails when Lotus data is selected as only 4 sets of data are provided. The message

Out of DATA at Line 100

indicates the failure to kind enough entries in the data table. Methods of overcoming the problem are given in section 27 which deals with error handling.

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001