For instance, this could be useful in FOR/NEXT loops. I realize that the standards situation with BASIC is sketchy. I am not looking for the exact answer, just any version of BASIC that might have included this functionality Update: I should have mentioned I have written a BASIC compiler, I wondered if there was a dialect of BASIC that supplied this builtin function. I added MAX_DATA() that seems similar to MAX_ROW/MAX_COL in some microcomputers. The first answer is a work-around without this capability..
Asked
Active
Viewed 54 times
1 Answers
4
You have basically two solutions to this problem, and only one of them involves using For/Next. The other involves using Do/Loop.
One method is to make the number of data elements the first data element, i.e.
DATA 10,A,B,C,D,E,F,G,H,I,J
READ Num
DIM D$(Num)
FOR I = 1 TO Num
READ D$(I)
NEXT I
Option two is to make the last data item something unique, and then check for the value. It can be numeric or string, whichever you prefer. For example if you're reading in prices, a negative could denote the end of the list. Note that in this case you either need to have the ability to REDIM PRESERVE or you need to dimension the array large enough to handle the maximum amount you'll need, or you need to process the data elements one at a time. (Or you'll need to read through the list twice, once to determine the number).
DATA 1.50, 2.95, -1
NumEls = 0
DO
READ Num
IF Num < 0 THEN
EXIT DO
END IF
NumEls = NumEls + 1
IF NumEls = 1 THEN
DIM P(1)
ELSE
REDIM PRESERVE P(NumEls)
END IF
P(NumEls) = Num
LOOP

Bill Hileman
- 2,798
- 2
- 17
- 24
-
It is interesting that both of these strategies were/are used for string types in other PLs. For instance, Pascal, at least Turbo Pascal, IIRC used the first method, where the first byte was the length of the string. Making 256 the max length of any string. In C, the null terminator at the end of the array of characters signaled the end of the string. So, likely Basic had a version of this with th READ/DATA statements. Also, you had to do your own type checking to make sure some data element was incompatible with the variable/array you were writing to. – edhowland Jan 05 '20 at 18:42
-
1That's an insightful comment. It is indeed safer to treat the variables as strings in such situations so that they can be checked for numeric values without error trapping. The idea of other languages having the first byte of a string (or two bytes for 64k strings) allows for compact memory usage whereas fixed-length strings would not. It's also why, I believe, that there is no intrinsic String data type in Java, but a String class because of the complexities of string editing internally - garbage collection in BASIC, etc. – Bill Hileman Jan 05 '20 at 20:29