Classic Computer Magazine Archive COMPUTE! ISSUE 74 / JULY 1986 / PAGE 104

The Beginners Page

Tom R. Halfhill, Editor

Turning Apples Into Oranges

Sometimes it seems as if BASIC is overpopulated with a nearly endless collection of string-handling functions. We've already looked at LEFT$, MID$, RIGHT$, CHR$, ASC, and LEN, not to mention operators for adding strings together and making logical comparisons. Yet, we've barely scratched the surface of what can be done with strings in BASIC. The reason for all these functions is that programmers keep demanding more and more flexibility for manipulating text in their programs. One of BASIC's early predecessors, a language called FORTRAN, was very strong on mathematical functions, but rather weak when dealing with strings. From the very beginning, BASIC has sought to improve in this area.

Two interesting string functions we haven't covered so far are VAL and STR$. These two functions, common to virtually all BASICs, are opposites of each other. STR$ lets you convert a number into a string, and VAL lets you convert a string into a number. For example, look at this program:

10 DIM A$(10):REM This line for Atari only
20 A$="123"
30 PRINT A$
40 A=VAL(A$)
50 PRINT A
60 A$=STR$(A)
70 PRINT A$

When you run this program, the result is:

123
123
123

So what? you say. We could get the same result by deleting lines 30--70 and simply substituting PRINT A$: PRINT A$:PRINT A$.

But the result would not be the same. Although PRINT A$ in line 30 prints the number 123 on the screen, it's printing the number as a character string, not a numeric value. You can't tell one from another when they're printed on the screen, but the difference is important and matters very much to BASIC. If you attempt a statement such as A = A + "123" or A$=A$+123, BASIC reports some sort of type-mismatch error. Character strings and numeric values just don't mix.

That's why there's VAL and STR$. As shown in line 40, A= VAL(A$) takes the number "123" stored as a character string in A$, converts it into the corresponding numeric value 123, and stores it in the numeric variable A. Conversely, the statement A$ = STR$(A) in line 60 takes the number stored as numeric value 123 in A, converts it into the character string "123," and stores it in the string variable A$. In short, VAL and STR$ let you turn apples into oranges and back again.

Easier Input

An interesting trick, you say, but what's the purpose? True, VAL and STR$ aren't exactly the most heavily used functions in the BASIC language. Instead, they're like Allen wrenches--once a year when you need them, nothing else will do.

For instance, recently I wrote a short program to experiment with a new computer's sound capabilities. The program asks the user to enter a phone number, then dials the number by playing Touch-Tones through the monitor speaker. To determine which Touch-Tone frequencies to play, I needed to extract each digit in the phone number as numeric data. But to make it as easy as possible for the user to enter the phone number, I wanted the program to accept the keyboard input as a character string. That way, people can type in the phone number any way they want: (919) 555-1212, or 919-555-1212, or 919 555 1212, etc. A statement such as INPUT A$ accepts all those variations. But if the program used INPUT A, the phone number would have to be entered as 9195551212, or an error would result.

So, VAL comes to the rescue. Once the phone number is entered in A$, the program loops through the string, converting digits from 0 to 9 into numbers with VAL. The numbers are then passed along to SOUND statements which play the tones. Spaces, hyphens, parentheses, and other characters are ignored. Here's a simplified version of the routine:

100	FOR N=1 TO LEN(A$)
110	IF ASC(MID$(A$,N,1))<48 THEN 150
120	IF ASC(MID$(A$,N,1))>57 THEN 150
130	A=VAL(MID$(A$,N,1))
140	REM SOUND statements to play Touch-Tones here...
150	NEXT N

This routine conveniently demonstrates several string-handling techniques we've covered in the past few columns. Line 100 sets up a FOR-NEXT loop by using the LEN function to measure the length of A$; this determines how many times the loop repeats (one loop for each character in A$). Lines 110 and 120 use the MID$ function to examine one character at a time in A$; if the ASC function discovers that the character is less than the ASCII value of 48 (the number 0) or greater than the ASCII value of 57 (the number 9), the program skips to line 150 and makes another pass through the loop.

If a character in A$ is a number 0-9, the program falls through to line 130. Here, the VAL function converts the character into a numeric value and stores it in A. Line 140 is where the SOUND statements to play the tones would be inserted. Line 150 then loops back to examine another character in A$.

You can adapt this trick to many of your own programs. Whenever you'd like to accept keyboard input as a string for maximum flexibility, just convert the string with VAL to the numeric input you're really looking for.