This article covers some of the same ground treated in Ken Brown‘s BASIC translation piece in the #5 COMPUTERIST. By comparing the two, you may learn more than you would from either one seperately.
In most texts introducing BASIC, the combination of the commands READ and DATA is one of the first terms introduced. It is also one of the most complex. Unfortunately, our computer can’t use it; if we want to enter a program that uses READ/DATA, we must use a combination of commands that we do have. ‘OK’ you say, ‘what combination of commands do I use ?’
It ain’t that simple. In the rest of this article, I will discuss some of the variety of ways READ/DATA is used, and some of the strategies we can use to meet the same requirements. By the end of the piece, I hope you will be able to recognize the general classes of data transfers that READ/DATA is used for, and will be able to generate T/S BASIC routines to do the same job.
READ/DATA is, as mentioned, a complex command, and it is used in a variety of ways. A single BASIC subroutine that can mimic all the potential of the original would be extremely cumbersome. In fact, since we are using programs from a variety of sources, our job is even bigger–each variety of BASIC has its own limitations on the READ/DATA function. The following applications are listed in rough order of complexity.
Repetitive calculations with a sequence of values are the computer’s forte, and that’s where READ & DATA are meant to shine. EX:
10 READ X
20 PRINT X
30 GOTO 10
40 DATA 12,14,6,30,3,40
50 END
Let’s follow what an Apple would do: first, it would encounter the READ X. To perform this command, it would then scan the program listing, starting from the first, searching for a DATA statement. Once found, it would take the first value after that, and assign it to the variable named in the READ line, leaving a flag behind to mark it’s place if a second READ is found. PRINT X is performed next, using the value in the variables file, not the DATA line. The DATA line itself is treated as a REM.
In standard BASIC, this is not an endless loop; for each READ statement, a flag is advanced along the DATA line; when the end of the line is reached, a test is made for a second DATA line; failing that, the program will terminate with an ‘OUT OF DATA’ error message.
There are two strategies to use in duplicating the above program. The first is simpler, but requires each data element to have a fixed length: the second allows variable length elements, but takes twice as much code.
Version 1:
10 LET D$="121406300340"
20 FOR N=1 TO LEN D$ STEP 2
30 LET X=VAL D$(N TO N+1)
40 PRINT X
50 NEXT N
60 STOP
Follow this through the cycle:
1. D$ is established, with the value “121406300340”
2. FOR-NEXT loop is set up to match D$: if D$ was created with an INPUT statement, the loop would adjust to the length given.
3. STEP value is set at the maximum number of digits needed; this means that each data element must have the same length (both 03 & 40 are two digits long).
4. X is set to the VAL of the slice of D$ pointed to by N (note that 03 and 3b (b=blank) have the same VAL)
5. PRINTed
6. N STEPped to the next element.
Depending on the particular data you are dealing with, you can establish an appropriate STEP length.
Next installment; I will cover variable length elements, and how to deal with READ’s that apply to two variables.
reprinted from CATS V1,4 (July ’83)
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.