The Beginners Page
Tom R. Halfhill, Editor
No Strings Attached
For the past few months, we've been discussing various kinds of numeric variables—those that store numbers. But BASIC has a second general type of variable that's worth knowing about, too—string variables.
Instead of storing numbers, string variables store characters. Characters can be letters of the alphabet, the numerals 0-9, punctuation marks, the foreign letters or graphics symbols found on some keyboards, spaces, and even special codes which have meaning only to computers.
In program listings, string variables resemble regular variables, but are denoted with a trailing dollar sign, as in A$ (pronounced "A-string"). Usually, all the rules that apply to numeric variable names on your computer's BASIC also apply to string variable names. For instance, the Commodore 64 allows variable names of any length, but the computer recognizes only the first two characters for purposes of telling them apart; ditto on the Apple; the IBM also allows names of any length, but recognizes the first 40 characters; the TI allows names up to 15 characters long, recognizing all 15; and the Atari allows names of any length and recognizes all characters.
String variables are easy to set up and use. You're probably already familiar with literal strings, such as the word HELLO found in the following program line:
10 PRINT "HELLO"
A literal string is analogous to a numeric constant—it doesn't change. PRINT "HELLO" always prints the word HELLO. But storing a string of characters in a string variable has the same advantages as storing a number in a numeric variable—your program can manipulate the variable (and therefore the characters it stores) at will. Here's a quick example:
10 A$ = "HELLO" 20 PRINT A$
(Atari users should add this line: 5 DIM A$(10). We'll explain why later.) When you run this program, it prints HELLO just like the previous program. But now add these lines:
30 A$ = "HI MOM!" 40 PRINT A$
Even though the PRINT statement in line 40 is identical to the one in line 20, it prints a different message: HI MOM! instead of HELLO. The reason, as you may have surmised, is that we assigned a new string of characters to the string variable A$ in line 30. In effect, we changed the "value" of A$ from HELLO to HI MOM!.
This is just a taste of how string variables can be modified by a running program. We'll cover many more possibilities over the next few columns. The important thing at this point is to grasp the advantage of string variables: They allow your programs to manipulate characters, words, and sentences instead of just numbers.
A DIM Memory
Take another look at the statements in lines 10 and 30 above. These are the string variable versions of assignment statements, just as the statement A = 10 assigns the value 10 to the numeric variable A. (In case you're wondering, the rarely seen keyword LET—as in LET A=10—can be used in string variable assignments, too, but is optional in almost all BASICs these days. It's customary to omit it.)
When you assign a string of characters to a string variable, BASIC stores the string in computer memory and uses the variable as a reference marker—sort of like the thumb tabs on the pages of a large dictionary. A program statement such as PRINT A$ tells BASIC to look up the string of characters in memory, retrieve it, and print it on the screen.
In TI-99/4A BASIC and most Microsoft-style BASICs (including those supplied with Commodore, Apple, and IBM computers), there's a limit on the length of the string that can be assigned to a string variable—255 characters. If you try to assign a longer string, you'll either get an error message or the string will be truncated (cut off) at the 255-character limit.
In Atari BASIC, a string can be of any length up to the limit of available program memory. On a 48K or 64K Atari with the Disk Operating System (DOS) and BASIC in memory, there's room for a string of more than 30,000 characters—although that wouldn't leave much memory for a very long program. Because the length of Atari strings is so flexible, Atari BASIC requires you to declare the maximum length of a string variable before using it in the program. Otherwise, the computer wouldn't know how much space to reserve for the string (Microsoft BASIC always knows that strings won't be longer than 255 characters).
The Atari BASIC statement for declaring a string's length is DIM(x), where x equals the maximum number of characters (DIM stands for DIMension). An example of the DIM statement is in line 5 above. It reserves memory for a string up to ten characters long, room enough for HI MOM! with a few characters to spare. The DIM statement must precede the first use of the string variable in the program, or you'll get an error. If you try to assign a string longer than the DIMed length, the string is truncated at the limit without an error message.
Next month we'll start delving a little deeper into how to use string variables in various ways in your programs.