Programming Hints
String Arrays
Stephen Levy
It is possible to simulate string arrays in Atari BASIC. The illustrations here show how.

"If you want string arrays on your computer, you'll just have to purchase Atari's Microsoft BASIC disk." A common belief, but not entirely true. You can create a string array using Atari BASIC. Microsoft BASIC does make the handling of arrays much easier, but it is possible to create a string array in Atari BASIC.

Creating the Array

What you will actually be creating is a long string which will hold all the elements of the array. In order that the array not have garbage in it, we must clean it out before using it.

There are two ways to clean out the string. The program below simply DIMensions a string to 1000 and then fills the string with "*" using a FOR/NEXT loop. Then it prints the string.

100 DIM B$(1000)
110 FOR A=1 TO 1000
120 B$(A,A)="*"
130 NEXT A
140 PRINT B$

The next program does the same thing a little differently and much more effeciently.

100 DIM B$(1000)
110 B$="*":B$(1000)="*":B$(2)=B$
120 PRINT B$

A lot faster, isn't it? You can use this mehtod anytime you want to fill a large string with the same character. That is exactly what we must do to begin creating our string array. But this time we need to fill the string with blanks.

Enter and RUN the program below. When the program asks for names, enter the names of ten friends, pressing RETURN after each. The program as written will allow names with up to ten letters.

100 DIM ARRAY$(100),ELEMENT$(10):PRINT CHR$(125)
110 ARRAY$=" ":ARRAY$(100)=" ":ARRAY$(2)=ARRAY$
120 FOR A=1 TO 10
140 ARRAY$(A*10-9,A*10)=ELEMENT$
160 PRINT 
200 FOR A=1 TO 10
210 PRINT "ARRAY$(";A;") IS ";ARRAY$(A*10-9,A*10):NEXT A
300 TRAP 340
330 PRINT ARRAY$(A*10-9,A*10):GOTO 310
340 PRINT CHR$(253):GOTO 300

Listing. String Arrays.
Download (Saved BASIC) / Download (Listed BASIC)

Notice that the program sets up an array with ten elements and allows you to pick from any of the ten. Let's look more closely at how it is done.

Line 100 DIMensions the array and clears the screen. Line 110 fills the array with blanks. Line 120 tells the computer to do it ten times. Line 130 gets your input.

Line 140 is the heart of the creation of the array. Within the parentheses the computer is told what part of the string should hold your input string ELEMENT$. The first time through A = 1; therefore, ARRAY$(A*10-9,A*10) will mean ARRAY$(1,10), or the first 10 positions in the string. When A = 2, we place ELEMENT$ in the positions 11 to 20 (2*10-9 = 1 and 2*10 = 20). We will continue to do this until the string is full.

Line 210 does the same thing, but in reverse order: it reads ARRAY$ and prints the proper part to the screen. Line 330 also does the same thing, but only for the part of the string you request.

Try this: RUN the program and enter any ten names. Then press BREAK. Type PRINT ARRAY$ without a line number, press RETURN, and see what happens.

Now, RUN the program again, but simply press RETURN without entering anything for the names. Notice that there appears to be nothing in ARRAY$. That is not really true—it is filled with blanks. Type PRINT ARRAY$ again and see what happens.

You might wonder what function lines 300 and 340 serve. Those two lines prevent the program from crashing when an incorrect INPUT is entered. TRAP 340 sends the program to line 340 instead of printing "ERROR 8 Line 320"when you enter a Q (or whatever) but the program requires a number between 1 and 10. Print CHR$(253) rings the buzzer, just as PRINT CHR$(125) in line 100 clears the screen.

Armed with this little bit of information, you now should be able to use string arrays in your own programs.

Return to Table of Contents | Previous Section | Next Section