INNOCENT INDEXING (ZX81)
by Tim Goldingham, Maidenhead, Berkshire
from Your Computer (October 1981), page 64

You have bought your Sinclair ZX-81, and now have it working but you know
nothing about programming. How can you make the magic machine do something
useful for you?

All you need to master for the time being is the PRINT statement. The
format is simple: first a line number, then the word PRINT generated by
pressing the P key, then a string of letters enclosed in inverted commas.
The secret is in the line number.

Lines of BASIC programs are identified by line numbers, and it is customary
to number them in tens  10, 20, 30 and so on  so that if you need to add
an extra instruction, you can give it an intermediate number, say, 25.

Built into the BASIC interpreter is a sorting mechanism which contrives to
list your instructions in ascending sequence, whatever order they are in
when you key them in. You can make use of this capability.

Suppose you want to build an index of telephone numbers. Try typing the
following lines, in the order shown:

    6000 PRINT "ST PANCRAS     387 9400"
    4000 PRINT "LIVERPOOL ST   247 7600"
    8000 PRINT "WATERLOO       928 5151"
    2000 PRINT "EUSTON         387 9400"
    5000 PRINT "PADDINGTON     723 7000"

Now look at the screen. What do you discover?  a sorted index. Press RUN,
and it will appear without the PRINT instructions.

All we have done is assign line numbers according to each item's place in
the alphabet. Now, of course, we can use the SAVE command to store the
index, and recall it by using LOAD.

For a simple directory, the method described should be all that is needed.
However, if you have a 16K store and want to make the most of it you can,
with a little more programming, adopt a rather more sophisticated approach.

A great advantage of this method is that it overcomes the limitations
imposed by Sinclair BASIC on the number of entries which could be stored
either as string variables or as elements in an array. By using PRINT
statements as in effect a data store, you can use almost the full range of
line numbers - up to 9999  less, of course, the few you allow for your
program.

This program uses the first three letters of each entry to generate a
precise line number, from 351 for AAA to 9139 for ZZZ.

The program will ask for the first three letters of the item you wish to
enter or refer to. Taking our previous example, you would type "ST " for
the first item, and "LIV" for the second.

The program then calculates a line number to spread the index entries over
the available range of numbers. In this example, "LIV" gives 4184. If you
are adding this as a new entry, you must, of course, check that the line
number has not been used before. The program therefore displays the
calculated number, followed by a listing of that area of the index. If the
number has not been used, you can create a new line of program,

    4184 PRINT "LIVERPOOL ST   247 7600"

If the number has already been used, you will have to use the nearest
available free number. Alternatively, if your index has a number of entries
beginning with the same initial letters, for instance, names beginning with
Mac, you could jump to a subroutine in the unused area between 9140 and
9959.

If you do not wish to make any changes or additions, but simply to refer to
the index, pressing "S" will list the index starting with the three-letter
key. To list it from the beginning, press NEWLINE on the initial prompt
without typing in any three letters. You may like to pick out the first
letter of each section by printing it in inverse video.

This program will run on the ZX-80, but is slightly less satisfactory than
on the 8K BASIC machine, as the 4K BASIC clears the screen before LISTing
the program. This means that you will have to remember the calculated line
number.

One or two changes are necessary to cater for the different formats of the
4K version. You will have to use TL$ in lines 90 and 110; and delete INT in
line 130 and AT 17,0 in line 200. Line 210 should be changed from PRINT to
INPUT C$; line 230 STOP is then superfluous.
