Classic Computer Magazine Archive COMPUTE! ISSUE 39 / AUGUST 1983 / PAGE 192


C. Regena

DATA, READ, And RESTORE Statements

Let's look at DATA and READ – what do these statements do and how do you use them? Using DATA statements in a program can save memory and may be more efficient than using many equivalent lines of code. However, a DATA statement can be more difficult to decode or understand because it can look like just a random group of numbers.

DATA statements are used in conjunction with READ statements. Together they assign numbers or strings to variable names.

100 READ N
110 DATA 5	is equivalent to	100 N =5

The DATA-READ concept becomes efficient when you assign several values to a variable name for a particular procedure. Let's look at an example:

100 	READ A
110 	PRINT A, A*A
120	IF   A=7  THEN  140  ELSE  100
130	DATA  3, 2, 6, 8, 7
140	END

When the program comes to READ A, the computer looks for the first DATA statement and assigns the first value, 3, to the variable A. The program continues, then comes to the statement READ A again. The computer has already read the first number, so it assigns the very next number, 2, to A. The process continues. Each time a READ statement is encountered, the very next data item in the DATA list is read, whether it is in the same DATA statement or the next DATA statement.

DATA Varieties

DATA statements may be placed anywhere in the program. They are ignored until a READ statement is executed. A "marker" is remembered by the computer so it knows exactly which data item has most recently been READ – and therefore which item the next READ statement will act upon.

A DATA statement may contain one item only or several items separated by commas. Data items may be numeric constants (numbers) or strings. Numbers may be positive or negative and may contain a decimal. Numbers may not be variable names and may not contain operators (such as 5/3). String variables do not need to be in quote marks unless there are leading spaces, trailing spaces, or embedded commas as part of the string. You may specify a null string by " ", or,, in a series. Example:

300 DATA " "JOHN,,,JIM," "

Line 300 contains six data items – null, JOHN, null, null, JIM, and null.

You may combine numbers and strings in the same DATA statements, but you must be careful that the data items in order match the READ statements. If the READ statement specifies a numeric variable, a string will not be accepted. You must have at least as many data items as the READ statements will try to access (or you will get an OUT OF DATA error). If you happen to have extra data items, they will be ignored.

A READ statement may specify one or several items. The items may be a combination of numeric and string variables. Keep in mind that READ statements only read the data and assign values to variables – later program lines would actually print, calculate, sort, or manipulate the data.

Following are some examples:

String Variables

100 FOR C=1 TO 5
110 READ A$
120 PRINT A$
130 NEXT C
150 END

Subscripted Numeric Variables

200 FOR I=1 TO 4
210 READ A(I)
220 PRINT "A(";I;")=";A(I)
230 NEXT I
240 DATA 32,-42,48,69,-73,89
250 END

Multiple Variables

300 FOR I=1 TO 3
330 NEXT I
340 DATA 12,24,42,8,8
350 DATA 35,20,15,38
360 END

To help conserve memory, a DATA statement can be up to four screen lines long (112 characters). You can edit and insert to make the line even longer. One exception is that if you have quite a few items separated by a lot of commas, the computer will accept only a little over three lines.

Printing Lowercase As An Example

The following program illustrates how DATA and READ statements are used to save memory in defining graphics characters. To specify each character number and definition in a separate CALL CHAR statement would require 26 statements. Using DATA and READ, four lines READ and define the graphics characters, and five data lines are used.

Program 1 redefines the small capital letters in characters 97 through 122 to graphics characters which can print actual lowercase letters. Letters with ascenders or descenders will require two letters to be printed. The chart shows which small capital letter (release the alpha lock key to print these) represents which graphic character. Lines 200-300 in the program illustrate how to print the lowercase letters.

Small Capital Letters And The Graphics Characters They Represent.

Program 1: Lowercase Letters

110 FOR C=97 TO 122
120 READ C$
130 CALL CHAR (C, C$)
140 NEXT C
150 DATA 3D4381818181433D, BCC2818181 81C2BC, 3C4280808080423C, 00000101 01010101, 3C4281FF8080423C
160 DATA 060908080808083E, 0101010141 221C,000080808080808,00000008, 08 08080808887,8890A0C0A0908884
170 DATA 0808080808080808.7884020202 020202, BCC2818181818181,3C428181 8181423C, 80808080808, 010101010101
180 DATA BCC281808080808, 3C42403C020 2423C, 0000080808087F08,818181818181433D, 4141222214140808, 0404888 85050202
190 DATA 8244281028448282, 10102020404, 7F0204081020407F
200 PRINT TAB (4) ; "1 "
210 PRINT TAB(4);"1 o vw e r {3 SPACES}c a s e"
220 PRINT : TAB(9) ; "1 {3 SPACES}h {3 SPACES}h{3 SPACES}t"
230 PRINT TAB(7);"a l b n a b e l"
240 PRINT TAB(11);"p"
250 PRINT :::" h{3 SPACES}d {3 spaces} f {3 spaces} h i i h l"
260 PRINT " a b c a e l a n l l k l n m"
270 PRINT TAB (13); "g {5 SPACES} j"
280 PRINT :TAB (13) ; "t"
290 PRINT " n o b a r s l u v vw x v z"
300 PRINT TAB(5); "p q"; TAB(24); "y"
310 GOTO 310
320 END


Now let's say you want to use a DATA statement to list some numbers. First you want to add the numbers, and then you want to multiply the numbers. The list of numbers for both processes is the same. To save memory (and typing effort), the TI allows you to RESTORE data. The RESTORE statement indicates that for the very next READ statement the computer will go back to the first DATA item in the program. RESTORE resets that "marker" to zero.

100   FOR   I=1   TO   5
110   READ  M,N
120  PRINT M;"+";N;"=";M+N
130  NEXT   I
140  PRINT
150  DATA  3,2,5,7,4,4,2,1,9,7
170  FOR   I=1   TO   5
180  READ  A,B
190  PRINT A;"*";B;"=";A*B
200  NEXT   I
210  END

RUN this sample program to see how the data items are used, then RESTOREd, then used again.

RESTORE can be very useful. TI BASIC also allows you to RESTORE to a certain line of data by specifying a line number. If you have a long program with lots of DATA statements, you can use a RESTORE n where n is a line number to make sure that each READ statement will read the correct data starting with the specified line of data.

This sample program illustrates the use of the RESTORE command. The DATA statements here contain duration factors and frequencies to be used in CALL SOUND statements. Ordinarily the first READ statement would read the first data items from the very first DATA statements. However, line 130 says to start reading the data in line 260 with the very next READ statement. Ten sounds are played; then we RESTORE 260 again so the ten sounds are repeated. Line 190 says RESTORE 240 so the data will start with line 240 for the very next READ statement.

Program 2: Sounds

120 FOR A=l TO 2
130 RESTORE 260
140 FOR I = 1 TO 10
150 READ T, F
160 CALL SOUND(T*50,F,2)
170 NEXT I
180 NEXT A
190 RESTORE 240
200 FOR I = 1 TO 22
210 READ T, F
220 CALL SOUND(T*100,F,2)
230 NEXT I
240 DATA 2,1046,2,784,2,659,4,523,2,440
250 DATA 2,392, 2,349,3,392,2,330,4,2
260 DATA 6,330,4,262,4,330,6,392,4,523, 4,494,6,523
270 DATA 4,392, 4,330,6,392
280 DATA 4,330, 8, 262
290 END

This "Southern States" program illustrates a variety of uses of DATA and READ statements. Keep in mind that the DATA statements can go anywhere in the program and are ignored until a READ statement is executed.

Note: As you are typing in programs from listings, the most likely place for bugs (errors) is in DATA statements. Be sure you copy DATA statements carefully. Watch particularly the placement of commas. Do not accidentally put a comma at the end of a DATA statement. If your data list consists of graphics definitions, those rounded characters are zeros, not the letter O. If your program stops with a BAD VALUE message, you can PRINT some of the variable names to see if you can pinpoint which DATA statement may be causing an error.

In any case, Southern States is an educational program that draws a map of the United States. One of the Southern States is outlined, and the user must type the name of the state. If the state is correct, the user must then type the name of the capital city. States are chosen in a random order. If you get the state and the capital right, that state will not appear again. However, if you miss an answer twice, the correct answer will be given and the state will appear again.

The data in lines 270-310 defines graphics characters for the map. We're using small capital letters so they can be printed, a faster method of drawing than using CALL HCHAR or CALL VCHAR. Be sure to release the alpha lock key to type in lines 320 and 480-510.

Line 330 (RESTORE 370) is not necessary the first time through the program because the data in line 370 would be the next data anyway. However, the program branches back to line 330 to RESTORE data if you'd like to try a "new" quiz. Lines 340-390 read the names of the states and the capital cities as the S$ array and C$ array.

Outlining States

Lines 540-560 randomly choose one of the states that has not previously been chosen and identified. The S$ value is set to " " (null) if the state is identified correctly. Depending on which state is chosen, certain data is RESTOREd (line 570 then lines 1500-2070).

Each state's data contains first a number representing the number of graphics characters that need to be defined. This number is READ in line 590 (READ N). Lines 600-630 then read the next data items to define the graphics characters. Line 640 reads N, the number of graphics characters that need to be placed on the map, and then lines 650-680 read the row coordinate, column coordinate, and character number from data to outline the state. To erase the state, line 1250 reads N, the number of characters needed to erase the state, and lines 1260-1290 read from the data the row coordinate, column coordinate, erasing graphic character, and number of repetitions. Most of the clearing is done with character 96, the plain yellow square, so repetitions can be used.

Program 3 Explained

110 Clear screen.
120-170 Define colors for graphics.
180-210 Print title screen.
230-310 Define graphics characters for map.
320 Define L$ for use in printing the map.
330-390 Read names of states in S$ array and corresponding capital cities in C$ array.
400-460 Print instruction screen and wait for user to press ENTER.
470-510 Clear screen and print map of United States.
520 Perform quiz for 11 states.
530 Initialize T, which keeps track of errors.
540-560 Randomly choose a state which has not previously been identified correctly.
570 Depending on state chosen, branch to appropriate RESTORE statement.
580 Clear four lines under map where answers will be typed.
590-630 Define graphics characters for particular state.
640-680 Outline state on map.
690-710 Ask for state.
720 Clear previous answer if incorrect.
730-810 Receive user's answer.
820-830 Beep then test answer.
840-940 If answer is incorrect, sound "uh-oh" and return for another answer. If answer is incorrect twice, print correct answer, wait for user to press ENTER.
950 If answer is correct, play arpeggio.
960-1230 Similar to state, ask for capital city, receive answer, test answer, branch appropriately.
1240 If state and capital are correct, S$(R) is set equal to null, "", so the state will not be chosen again.
1250-1290 Erase the state.
1300 Return for next state to be identified.
1310 Clear printing.
1320-1370 Print option to try again and branoh appropriately.
1380-1440 Subroutine to print "PRESS ENTER" and wait for user to press ENTER.
1450-1490 Subroutine to play music for correct answer.
1500-1560 RESTORE data for Texas.
1570-1620 RESTORE data for Oklahoma.
1630-2060 RESTORE data for Arkansas, Louisiana, Tennessee, Mississippi, Alabama, Florida, Georgia, South Carolina, and North Carolina.
2070 END.

If you prefer to save typing effort, you may receive a copy of Program 3 by sending $3, a blank cassette or diskette, and a stamped, self-addressed mailer to C. Regena, P.O. Box 1502, Cedar City, UT 84720. Be sure to specify "Southern States" for the TI-99/4A computer.