
A ship with four rooms needs four names. You could call them ROOM1$, ROOM2$, ROOM3$ and ROOM4$ - but then there is no way to say "room number R", and no way for a loop to go through them. What you want is one name for the whole list, and a number to pick out one thing on it. That is an array.
DIM ROOM$(4)
makes a list of strings called ROOM$. Each one is picked out by a number in brackets: ROOM$(1), ROOM$(2) and so on. Each works exactly like an ordinary string variable:
ROOM$(1)="CRYO BAY"
PRINT ROOM$(1)
The number in brackets does not have to be written out. ROOM$(R) means "the one whose number is in R" - and that is what makes arrays worth having: a FOR loop can go through every one.
Arrays of numbers work the same way, without the $: DIM SCORE(9).
DIM ROOM$(4) makes five elements, not four: ROOM$(0) to ROOM$(4). The numbering starts at 0. You can use element 0 or ignore it - this course numbers its rooms from 1, because "room 1" reads better than "room 0", and lets element 0 sit empty.
When an array is made, every number in it is 0 and every string is empty.
Ask for ROOM$(5) in a list that stops at 4, and BBC BASIC stops with Subscript. Use an array before its DIM and you get Array. And an array can only be DIMmed once in a run.
10 DIM ROOM$(4)
20 ROOM$(1)="CRYO BAY"
30 ROOM$(2)="CORRIDOR"
40 ROOM$(3)="ENGINEERING"
50 ROOM$(4)="BRIDGE"
60 FOR R=1 TO 4
70 PRINT R;" ";ROOM$(R)
80 NEXT R
Starting from
A fresh boot, or NEW.
What you should see
>RUN
1 CRYO BAY
2 CORRIDOR
3 ENGINEERING
4 BRIDGE
>
These are DRIFT's four rooms. The game keeps them in a list just like this.

55 ROOM$(5)="AIRLOCK" and run it. What happens, and why?FOR R=0 TO 4. What is in element 0?14.1 Ask for five scores, keep them in an array, and print their average.
14.2 Print the four rooms in reverse order.
14.3 Ask for a room number from 1 to 4 and print that room's name.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
Subscript | A number in brackets past the end of the array - or below 0. |
Array | The array is used before it has been DIMmed. |
| An element is empty or 0 when you expected something | Nothing was ever put in it - element 0, perhaps. |
DIM NAME$(n) makes a list with elements 0 to n; NAME$(R) is element R, and R can be any sum. A loop can work through the whole list. Past the end is Subscript; before the DIM is Array.
S15, Ready-Made Data - filling a list from values written into the program.