Using Data Statements

See all articles from CATS v2 n12
Author(s): Ward Seguin
Publication: CATS Newsletter
Pub Details: CATS v2 n12 (Volume 2, Issue 12)
Date: March 1985
Pages: 14

Programming Tip

Data statements are handy places to store numbers or strings in a program. Using the READ statement information stored in data statements can be assigned to variables or arrays.

The manual points out that there can be a number of data statements in a program but they are treated as a single data list. A brief discussion of data statements is found on pages 108 and 109 in the 2068 manual.

If you know how many elements are in the data statements, you can write your program so that it is specifically instructed to read that fixed number of elements. If you don’t know how many elements are in the data statements or you would like to keep the program general so that you can add additional elements in the future and still have the program execute, there is another way.

Suppose your data statements contain lists of items in your budget. For example:

  10 DATA "Mortgage", "Food"
20 DATA "Doctor", "Medicine"

And suppose you are reading these into an array: A$(x,y). Recall x specifies the element in the array and y specifies the length. In the above example, we have four elements the longest of which is eight letters long. READ A$(1) would read Mortgage into this first array location, READ A$(2) would read Food into the second and so on. Incidentally, the array A$ would have to be dimensioned at the beginning of the program for the largest number of elements and the longest word you expect to enter into it sometime in the future.

The question is, how can you write a general DATA statement and READ it so that it will only read the number of elements in the data statements. Here’s how. Write one more data statement that will always be your last: e.g.

  50 DATA "End"

Then after each read, perform the following test:

  60 READ A$(I)
70 IF A$(I,1 TO 3)= "End"
THEN GOTO 99
80 LET I=I+1
90 GOTO 60
99 CONTINUE

Statement 70 compares the first three letters of each element. Is is important to note that the statement –IF A$(I) = “End”– will not work because “END” in A$(I) is 8 characters long, three letters followed by five blanks.

The above example allows you to read in all of your data and to stop reading when you encounter the word End. You can then use the value of I to tell you how many elements were in the DATA statements. If you add additional budget categories in the future, your program should accept them provided your initial dimension statements anticipated the largest number of elements and the longest word. A similar scheme can be devised if your DATA statements contain numbers.

Ward Seguin

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.