DIM function

Apart from the DIM array declaration command, there's also a function by the same name that returns information regarding any declared array. It's syntax is:

DIM (arrayname$ [, dimension]) |numeric and it returns the number of elements in the specified dimension of the array arrayname (dimension defaults to 0 if not specified).

If dimension equals 0, DIM will simply return the number of dimensions in the array.

Simple strings are treated as single-dimension character arrays, returning 1 as the number of dimensions and the current string length as the number of elements in dimension 1.
Let's write a little program to illustrate:

10 DIM a(100,10,5)
20 PRINT DIM (a())
30 PRINT DIM(a(),1)
40 PRINT DIM(a(),2)
50 PRINT DIM(a(),3)

which will return:

  3
100
 10
  5

Exercises

  1. Use READ and DATA statements to set up an array m$ of twelve strings in which m$(n) is the name of the nᵗʰ month. (Hint: the DIM statement will be DIM m$(12,9). Test it by printing out all the m$(n) (use a loop)).

  2. Type:

    PRINT "now is the month of
    ";m$(5);"ing"; " when
    merry lads
    are playing"
    

    What can you do about all those spaces?


ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.