Procrustean - another way of thinking of them is as arrays (with one extra dimension) of single characters.
The name of a string array is a single
letter followed by $, & a string array & a simple string variable cannot have the same name (unlike the case
for numbers).
Suppose then, that you want an array A$ of five strings. You must decide how long these strings are to
be - let us suppose that 10 characters each is long enough. You then say
DIM
A$(5,10) (type this in)
This sets up a 5*10 array of characters, but you can also think of each row as being a string:
A$(1)=A$(1,1) A$(1,2) ... A$(1,10)
A$(2)=A$(2,1) A$(2,2) ... A$(2,10)
: : : : : :
A$(5)=A$(5,1) A$(5,2) ... A$(5,10)
If you give the same number of subscripts (two in this case) as there were dimensions in the
DIM
statement, then you get a single character; but if you miss the last one out, then you get a fixed length
string. So, for instance, A$(2,7) is the 7th character in the string A$(2); using the slicing notation, we could
also write this as A$(2)(7). Now type
LET
A$(2)="1234567890"
&
PRINT
A$(2),A$(2,7)
You get
1234567890 7
For the last subscript (the one you can miss out), you can also have a slice, so that for instance
A$(2,4
TO
8) = A$(2)(4
TO
8) = "45678"
Remember:
In a string array, all the strings have the same, fixed length.
The
DIM
statement has an extra number (the last one) to specify this length.
When you write down a subscripted variable for a string array, you can put in an extra number, or a
slicer, to correspond with the extra number in the
DIM
statement.
Summary
Arrays (the way the ZX81 handles string arrays is slightly non-standard.)
Statements:
DIM
Exercises
1. Set up an array M$ of twelve strings in which M$(N) is the name of the Nth month. (Hint: the
DIM
statement will be
DIM
M$(12,9).) Test it by
printing out all the M$(N) (use a loop). Type
PRINT
"NOW IS THE MONTH OF ";M$(5);"ING";"WHEN MERRY LADS ARE PLAYING"
What can you do about all those spaces?
2. You can have string arrays with no dimensions. Type
DIM
A$(10)
& you will find that A$ behaves just like a string variable, except that it always has length 10, & assignment
to it is always Procrustean.
3. READ, DATA & RESTORE; who needs them?
Most BASICs (but not the ZX81 BASIC) have three statements called READ, DATA & RESTORE.