Classic Computer Magazine Archive ANTIC VOL. 2, NO. 11 / FEBRUARY 1984

PHONE BOOK

Simulate string arrays in Atari BASIC

by JERRY WHITE

Many ANTIC readers have asked how to simulate a string array in Atari BASIC. This program, called TELEPHON, will serve as a tutorial on string arrays and string manipulation. But for those of you who just want to type in and use this handy little program, I'll begin with some instructions.

PROGRAM INSTRUCTIONS

TELEPHON will run on any Atari computer with BASIC installed. Either a cassette recorder or disk drive is required. A printer is optional. The program begins by asking where your data will be stored. Type [C] for cassette or [D] for disk. TELEPHON uses INPUT commands and does not have a sophisticated keyboard handler. Be sure to respond using only normal (not inverse), uppercase characters, and press the [RETURN] key after each entry.

A menu of choices will appear on the screen. A display near the top of the screen shows the number of records currently in memory and the number of records available. A record consists of a name and a telephone number. The information in each record is broken up into five "fields," as shown in Table 1. Also shown are the corresponding variables and lengths for each field.

Table 1.
FIELD_____STRING______MAXLEN
Last name__LAST$_______12
First name__FIRST$_______12
Area code__AREA$________3
Exchange___EXCH$_______3
Number____PNUM$_______4

We do not yet have a data file, so we begin with Menu Choice 1, which directs the program to enter new data. This is also the choice to use whenever you add records. After you have created and saved a TELEPHON data file by writing it onto tape or disk, you will begin each session by using Menu Choice 2 to load your data into memory.

You will then be prompted to enter data into the fields described above. After the five items have been entered, the program will return to the menu screen. Notice that the display near the top of the screen now indicates that one record is in memory and that one fewer record is available. Enter a few more records using Menu Choice 1, but not too many. We want to make sure that your program is working perfectly before you type in your whole telephone book.

After entering a few records, save your data by using Choice 3 on the menu. A message will appear on the screen that tells you to press the [START] key when your input/output device is ready. Cassette users should insert a blank tape, rewind it to the beginning, and then press the [PLAY] and [RECORD] buttons simultaneously. Disk users must make sure that a diskette with an ample number of free sectors is ready in Drive 1. Our program will use the disk filename "TELEPHON.DAT" but you can choose your own filename. When the [START] key is pressed, cassette users will hear the familiar series of beeps. Press [RETURN] to begin the process of writing your data onto tape.

Whenever you load data using Menu Choice 2, any records in memory will be lost. Be sure that you always use this choice first.

If you make an error while entering data, Choice 4 allows you to correct it. The program will ask for the "last name" of the record to be altered, and will search through each record until a match is found. Upon finding a match, the program displays an entire record. If this is the record to be altered, respond with [Y]; if not, respond with [N]. If you type [N], the search will continue. If you type [Y], you may then reenter the data correctly. This procedure can also be used to update your files when your friends change their telephone numbers.

Menu Choice 5 tells the program to sort your data by last name. This makes it easier to find a specific record when you print your data using Menu Choice 6.

Those of you with printers will find this program particularly useful. You will be asked to type [S] to display your telephone directory on the screen, or [P] to include a listing to your printer. The listing can replace your old personal telephone book.

To freeze the display while using Choice 6,press the [CTRL] and [1] keys. Repeat this procedure to continue.

Menu Choice 7 ends the program and returns control to the BASIC cartridge. TELEPHON was written entirely in Atari BASIC. The [SYSTEM RESET] and [BREAK] keys function normally when it is in use. If you accidentally press the [BREAK] key, you can return to the menu screen by entering the following command in immediate mode:

GOTO MENU

If you'd like to disable the [BREAK] key, add the following line to the program:

14001 POKE 16,64:POKE 53774,112

A WORD OF WARNING

Before we move on to the string array tutorial and some of the more technical information, there is one important point that you should understand. TELEPHON dimensions a string called ARRAY$ according to the current amount of free memory in your Atari. This will leave very little RAM available for additional lines of code. If you [BREAK] into the program to add code, you should first issue a CLR command in immediate mode. This will make all of the string area available again, but it will also erase any data you have in memory. Therefore, you should always SAVE your data, and the program itself, before making any changes to the program.

SIMULATING STRING ARRAYS

Atari BASIC is often derided by people who are familiar with Microsoft BASIC because it doesn't offer string arrays, although it does feature virtually unlimited string length and numeric arrays. BASIC XL, from Optimized Systems Software, offers both forms of string handling. But if you have only Atari BASIC, don't despair. String arrays can be simulated, and the TELEPHON program is a good example of this.

H0W TELEPHON WORKS

TELEPHON uses 34 characters for each record. The first12 positions are assigned to the last name, and unused positions are padded with blanks. Positions 13 through 24 are used for the first name, 25 through 27 for the area code, 28 through 30 for the exchange, and 31 through 34 for the remaining four digits of the telephone number.

Our data is stored in the string called ARRAY$. ARRAY$(1,34) contains our first record, and is the equivalent of the first element in a Microsoft array. The equivalent of array element number two would be ARRAY$(35,68). To find the tenth element (record) in Atari BASIC, we can simply set a variable that is equal to our record length multiplied by 10, and then print the record using the following routine:

STRPOS = 10*34:

?ARRAY(STRPOS,STRPOS+33)

MEMORY-SAVING TECHNIQUES

Our data and the TELEPHON program must share the computer's memory, so I've kept the program as small as possible. If the program used all available RAM, there would be no room for data. As written, TELEPHON can store over 800 records of data on a 48K machine. If you delete the REM statements, you can salvage enough memory for about a dozen additional records.

One of the other memory-saving techniques I used in TELEPHON was to use the variable O instead of the number 0 and the variable I instead of the number 1. Six bytes of RAM are saved each time that a numeric variable replaces a number. These variables and their numeric values look similar, so they don't detract very much from program readability.

By studying this program carefully, you can learn how to manipulate strings in Atari BASIC. Table 2 provides a listing of previously undefined key variables and strings, along with brief definitions. This will help you understand the program's logic.

Do you have any other questions on Atari BASIC? Would you like to see other programs like this one in future issues of ANTIC? Please send your questions and comments to Jerry White in care of ANTIC Magazine.

Table 2
VAR/STR$____DEFINITION
ARRAY_______Maximum number of records
BLANK$______Stores 34 spaces
CHOICE_______Numeric value of a chosen option
CLEAR_______Clear-screen routine (line 14000)
DEV$________Stores C: or D: TELEPHON.DAT
DING________Sound routine (line 17000)
INLEN________Length of keyboard input
MENU________Menu routine (line 12030)
PAPER_______Printer flag
REC$________Temporary record storage
RECLEN______Record length of 34
RLM1________Record length minus 1
STRPOS______String position
TABLEN______RAM location 201 (tab length)
TOTAL_______Current total of records in memoy
WORK$_______Temporary storage used by sort

Jerry White is a Contributing Editor to ANTIC. His programs are available from several software companies, notably Adventure International and Educational Software.

Listing: TELEPHON.BAS Download