BBC BASIC 2.31 for the Tatung Einstein, showing DRIFT, the text adventure the course builds, in the middle of a game
← Back to Courses
module
15

Ready-Made Data

Introduction

S14 filled the list of rooms with four assignments, one per line. For four rooms that is fine. For a map with rooms, exits, descriptions and objects it is page after page of ROOM$(3)=. BBC BASIC has a neater way: write the values out as a list, and have the program read them.

DATA And READ

80 DATA CRYO BAY,CORRIDOR,ENGINEERING,BRIDGE

is a line of values, separated by commas. It does nothing on its own. READ takes the next value from it:

READ ROOM$(R)

The first READ takes CRYO BAY, the next CORRIDOR, and so on, in order. BBC BASIC keeps its place, across as many DATA lines as there are, in line number order.

DATA can hold numbers as well as words, and one READ can take several things: READ R$,E takes a word and then a number. The DATA has to give them in the same order.

Words in DATA do not need quotes - unless they have a comma in them. A comma in an unquoted item ends it: ENGINEERING, DECK 2 is read as two items, ENGINEERING and DECK 2. Put it in quotes, "ENGINEERING, DECK 2", and it is one.

Running Out

READ once too often and BBC BASIC stops with Out of DATA, naming the line with the READ.

RESTORE

RESTORE sends the reading back to the first DATA in the program, so the values can be read again. RESTORE 100 starts from the DATA at line 100 instead - useful when a program keeps several lists.

The Code

10 DIM ROOM$(4)
20 FOR R=1 TO 4
30 READ ROOM$(R)
40 NEXT R
50 FOR R=1 TO 4
60 PRINT ROOM$(R)
70 NEXT R
80 DATA CRYO BAY,CORRIDOR,ENGINEERING,BRIDGE

Starting from

A fresh boot, or NEW.

What you should see

>RUN
CRYO BAY
CORRIDOR
ENGINEERING
BRIDGE
>

The same four rooms as S14, but the names are in one place, and adding a room means adding a word to line 80. DRIFT's whole map will be DATA.

Rooms read from DATA

Change One Thing

  • Change line 20 to FOR R=1 TO 5. What does BBC BASIC say, and which line does it name?
  • Put line 20 back, and make the third room "ENGINEERING, DECK 2", with the quotes. Then take the quotes off. What is printed each time, and what has happened to the bridge?
  • At the >, try PRINT ROOM$(-1). What does that tell you about the bottom end of an array?

Exercises

15.1 Put five numbers in a DATA line, read them, and print their total.

15.2 Put each room's name and how many ways out it has in DATA, and print CORRIDOR HAS 3 EXITS and so on for all four. (The corridor has three; the others one each.)

15.3 Read the same DATA item twice, using RESTORE.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
Out of DATAMore READs than values. Count them.
An item is split in two, and everything after it is one place outIt has a comma in it. Put it in quotes.
No such variable at a READA number was wanted and the DATA had a word there - BBC BASIC took the word for the name of a variable. The DATA is not in the same order as the READ. (The other way round there is no error: a number read into a string arrives as its digits.)
SubscriptAn array number below 0 or above its DIM.

Summary

DATA lines hold a program's values; READ takes them one after another, into any kind of variable. Quote an item with a comma in it. RESTORE starts again, RESTORE n from line n.

Next

S16, Chance - numbers nobody can predict.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.