Classic Computer Magazine Archive ANTIC VOL. 7, NO. 2 / JUNE 1988

String Array Simulator

List processing power pick-up

By RICHARD WHITSELL

For tracking lists of names and numbers, arrays of strings come in very handy. But unlike most other BASIC programming languages, Atari BASIC doesn't provide for string arrays. So if you're an intermediate programmer you'll just have to fool BASIC with String Array Simulator. This BASIC program works on all 8-bit Atari computers of any memory size, with disk or cassette.



Consider a "Battleship" game board. It is an indexed collection of holes into which we may place one peg per hole. In other words, it is an array of holes which holds our pegs, a "peg array."

Similarly, an array in Atari BASIC is an indexed collection of variables into which we may place numbers, one number per variable. Such arrays are handy for storing tables of numbers, such as multiplication tables.

To store text, string variables are commonly used. A string is an uninterrupted set of bytes whose length and address is known to the computer. In Atari BASIC, string variable names always end with a '$' character, such as X$, AB$ and Q$.

Most versions of BASIC, except Atari BASIC, let you use arrays of strings as well as arrays of numbers. These are indexed collections of string variables into which we may place strings, one whole string per variable.

FOOLING BASIC

The first thing I noticed about Atari BASIC was that it didn't handle arrays of strings. The reason is that Atari BASIC considers each string to be a one-dimensional array of characters.

For example, if A$="ABCDEFGHIJ", then A$(4,8) would give you "DEFGH", the fourth through eighth characters in A$. This really causes headaches for Atari BASIC programmers who want to manipulate lists of data that can only be put into strings.

But now there is a way to get around this limitation. We'll use a BASIC subroutine to simulate a string array. All the main program must do then is set a few variables and then GOSUB to the subroutine.

First, we'll DIMension a storage string, making sure it is large enough to hold our array of strings. Let the subroutines worry about positioning and retrieving the substrings within the array.

There are two ways to manipulate substrings. The only difference between methods is the way they track individual string lengths.

METHOD 1

The first method stores the length of the substring within the substring itself. Any length, from 0 to 255, can be stored as an ATASCII value in the first character of each substring.

Type in Listing 1, STRING1.BAS, check it with TYPO II and SAVE a copy before you RUN it. When RUN, it will prompt you to type 10 names. Press [RETURN] after each name. The completed list will be displayed in a table with the name lengths in a separate colunin.

Heres how the program works:

Line 10 defines the constant MAXNUM = 10. This is the maximum number of strings you want in the array. MAXLEN=20+1 defines the maximum length of each string in the array, plus one character to hold the length.

Line 20 DIMensions the storage string to hold the maximum number of strings at their maximum length.

Line 30 DIMensions; WORK$ to be the maximum length of any of the strings.

You only need two variables to call the subroutines: INDEX and WORK$. INDEX is the index into the array. It tells the subroutines which string you want. Since arrays are traditionally based at zero, the index can be from 0 to 9 and the maximum number of strings is 10.

WORK$ is a temporary string for passing data to and from the subroutines.

To retrieve a string, set the value of INDEX and then GOSUB 5000. The string you want will return in WORK$. To store a string, set the value of INDEX, put your string in WORK$ and then GOSUB 6000.

If you look at the subroutines in STRING1.BAS, you get an idea of how the length is stored in the "array." That's why lines 5020 and 6020 read START= START+ 1, to skip over that length character before retrieving the string.

METHOD 2

The second method works just as well. As a matter of fact, you use the same two variables, INDEX and WORK$, that you used in the first method.

Type in Listing 2, STRING2.BAS, check it with TYPO II and SAVE a copy before you RUN it. This method stores the lengths in LENARRAY, an array of numbers specifically reserved for that purpose.

In the second method, Line 10 has been changed to show MAXLEN = 20 instead of MAXLEN = 20 + 1, because now we don't need that extra character to store the length.

Line 35 DIMensions the array of lengths to be used by the subroutines.

By the way, if you want to use strings longer than 255 characters, STRING2.BAS can do it without modification. But you'll have to change STRING1.BAS to store an extra character for the length.

Also, if you're not going to use all the strings in the array right away, it's a good idea to set all the strings to null (WORK$ = "") at the start of your program. This will keep you from pulling out garbage characters where you haven't stored a string.


Richard Whitsell of Norman, Oklahoma is a computer engineering student who programs on both the Atari 800 and the ST.

FOR MORE ARTICLES LIKE THIS, CIRCLE 201 ON READER SERVICE CARD.

Listing 1: STRING1.BAS Download

Listing 2: STRING2.BAS Download