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
14

Lists Of Things

Introduction

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

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).

Counting From Nought

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.

The Edges

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.

The Code

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.

The four rooms, from an array

Change One Thing

  • Add 55 ROOM$(5)="AIRLOCK" and run it. What happens, and why?
  • Take line 55 out again, and change line 60 to FOR R=0 TO 4. What is in element 0?
  • Put line 60 back, and delete line 10. What does BBC BASIC say now?

Exercises

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.

When It Goes Wrong

SymptomCause
SubscriptA number in brackets past the end of the array - or below 0.
ArrayThe array is used before it has been DIMmed.
An element is empty or 0 when you expected somethingNothing was ever put in it - element 0, perhaps.

Summary

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.

Next

S15, Ready-Made Data - filling a list from values written into the program.

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.