
Good morning, class! Happy St. Patrick’s day to you all.
What’s this on my desk – a six pack of bottled green beer. Just what I wanted. No need to guess who brought it. You can all turn around and stop smiling at him.
Many of you were not in our first class back in April of 1982 when we discussed simulating READ, DATA and RESTORE commands.
I’ll give you a sample of a program that would work on most other computers but not on the Sinclair machines.
* UNUSABLE PROGRAM *
10 DATA JANUARY,FEBRUARY,MARCH,
APRIL,MAY,JUNE,JULY,AUGUST,
SEPTEMBER,OCTOBER,NOVEMBER,
DECEMBER
20 FOR N=1 TO 12
30 READ A$
40 PRINT A$
50 NEXT N
60 RESTORE
70 GOTO 20
If we were to RUN this program on most other computers, the following list would be displayed on the CRT:
JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER OCTOBER NOVEMBER DECEMBER
The DATA command allows us to insert a data file in a program line. The READ command allows us to pick out and separate these data items, one at a time. The RESTORE command resets the pointer that is locating each data item back to the first data item.
The purpose of these three commands is to allow the programmer to store data in the program lines themselves for recall at a later time.
Let us say that we only wanted to see one particular month. We might add or change these lines:
15 PRINT "WHAT IS THE NUMBER OF
THE MONTH YOU WANT TO SEE?"
16 INPUT A
40 IF N=A THEN PRINT A$
Now when the program is RUN, we ENTER the number of the month we want to see, such as the number <5> and we are greeted with the display of only that month on the CRT:
MAY
Of course, most of you do not have other computers to test this on so it’s all academic up to this point, anyway.
The point I’m trying to make is that we have here a very efficient method of retrieving data from the program with a minimum of program lines being used.
We could, of course, accomplish this on the ZX/TS machines with a program using a string variable array such as the following:
* USABLE PROGRAM BUT *
* INEFFICIENT *
10 DIM A$(12,9)
20 LET A$(1)="JANUARY"
30 LET A$(2)="FEBRUARY"
40 etc.
and recall the months with a FOR/NEXT loop. As you can see we use up 12 program lines just to enter the data into variables, not to mention the FOR/NEXT loop and other program lines that will be needed. There’s got to be an easier way – there is. ENTER the following program:
* USABLE EFFICIENT PROGRAM *
10 LET A$="JANUARY,FEBURARY,MA
RCH,APRIL,MAY,JUNE,JULY,AUGUST,S
EPTEMBER,OCTOBER,NOVEMBER,DECEMB
ER,"
20 LET M=1
30 FOR N=1 TO LEN A$
40 IF A$(N)="," THEN GOSUB 100
50 NEXT N
60 STOP
100 LET B$=A$(M TO N-1)
110 PRINT B$
120 LET M=N+1
130 RETURN
This accomplishes the same thing that the first, unusable program does but in 10 lines instead of the original 7 – a considerable savings of programming effort over the method that assigns data to the string variable array.
You may also notice that in line 10 of that program, we have set up the string <A$> to allow 12 inputs of a maximum length of 9 characters each. The 9 character maximum length is to allow the month of SEPTEMBER to be used. But all the rest of the months have less than 9 characters. MAY has only 3 characters. We have to dimensionalize the array to allow the maximum amount of characters that it will use. If we have less, it will truncate or chop off any characters over the stated amount from the end of the word.
We therefore have all those empty spaces that aren’t used and guess what? That’s right, they take up memory space. This means our program size is going to be larger and we are penalized as to the amount of data items we can have. The last program is much more efficient in this respect.
But what if we want to single out an individual month or data item like we did in the unusable program? We have to be tricky here. Add or change the following lines:
15 LET B=0
16 PRINT "WHICH MONTH DO YOU W
ANT TO SEE?"
17 INPUT A
105 LET B=B+1
110 IF B=A THEN PRINT B$
Now if we RUN the program and ENTER <5> to the prompt, we will see MAY displayed. This is about where we left off last April. A very powerful routine, indeed, and the breakout portion of the routine as evidenced by those last five lines can be used in other types of READ, DATA & RESTORE simulation, as we shall soon see.
Perhaps we are not concerned with the DATA being stored in a program line. We may not even care if it is lost after we shut the computer off and reload the original program back in at a later date. A good example would be where we need to store data that is being created by the program but have no need for that data after it was printed out or copied from the screen or possibly it was just random data for a display example. We can POKE that data into higher memory locations that are not being used by the main program.
How do we know where those locations might be? The gentleman in the rear has his hand up.
Yes, that is correct. We can use the routine from the February/83 Computer Tutor to find the program size of the main program in bytes, add that amount to the number 16514 and and that would be the first location of free memory space available. For purposes of our next program routine, let us say that the next available memory location would reside at the decimal address of 32000 (16K machines only – 2K users use 18000 where 32000 appears). Clear your computer (SAVE the last program if you want to) and type in the following program:
10 LET S=32000
100 FOR N=1 TO 10
110 SCROLL
120 PRINT "INPUT NAME NO.";N
130 INPUT A$
140 SCROLL
150 PRINT ,A$
160 SCROLL
170 PRINT ,,
180 FOR I=1 TO LEN A$
190 POKE S,CODE A$(I)
200 LET S=S+1
210 NEXT I
220 POKE S,255
230 LET S=S+1
240 NEXT N
250 POKE S,255
500 SCROLL
510 SCROLL
520 PRINT "PRESS ENTER TO SEE L
IST :::"
1000 LET S=32000
1010 LET B$=""
1020 IF PEEK S=255 THEN GOTO 110
0
1030 LET B$=B$+CHR$ (PEEK S)
1040 LET S=S+1
1050 GOTO 1020
1100 SCROLL
1110 PRINT B$
1120 SCROLL
1130 IF PEEK S+PEEK S+1=510 THEN
STOP
1140 LET S=S+1
1150 GOTO 1010
Now RUN the program and ENTER 10 names. The program will poke each of them into the higher memory locations. When you respond to the prompt to SEE LIST, the individual names are displayed.
LIST the program and you, of course, cannot see any of the names in any part of the listing. Previous programming techniques have shown us that data can be stored in string or numeric variables and it will remain there until we type in RUN or CLEAR. Then, all variables are reset to null and if we ask the computer to print any of those variables, we will get an UNASSIGNED VARIABLE error message.
You are about to witness something strange. ENTER the CLEAR command and the ENTER GOTO 1000.
The data is still there. Now ENTER CLEAR again and then ENTER RUN 1000. Same thing! Why?
The data is fixed in the assigned memory locations which has nothing to do with the variables. Neat, huh?
In the immortal words of Albert Einstein, “you aint seen nutting yet”! ENTER the following program (16K only – SAVE the old one if you wish):
10 REM .......................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
20 LET S=16514
30 LET N=1
100 SCROLL
110 SCROLL
120 PRINT "INPUT NAME NO.";N
130 INPUT A$
135 IF A$="" THEN GOTO 260
140 SCROLL
150 PRINT ,A$
160 SCROLL
170 PRINT ,,
180 FOR I=1 TO LEN A$
190 POKE S,CODE A$(I)
200 LET S=S+1
210 NEXT I
220 POKE S,26
230 LET S=S+1
240 LET N=N+1
250 GOTO 100
260 POKE S,255
500 SCROLL
510 SCROLL
520 PRINT "PRESS ENTER TO SEE L
IST :::"
530 INPUT A$
1000 LET S=16514
1010 LET B$=""
1020 IF PEEK S=26 THEN GOTO 1060
1030 LET B$=B$+CHR$ (PEEK S)
1040 LET S=S+1
1050 GOTO 1020
1060 SCROLL
1100 SCROLL
1110 PRINT B$
1120 SCROLL
1130 IF PEEK S+PEEK (S+1)=281 TH
EN STOP
1140 LET S=S+1
1150 GOTO 1010
2000 FAST
2010 LET G=695
2020 FOR N=16514 TO 16513+G
2030 POKE N,155
2040 NEXT N
2050 SLOW
Yup, that’s 695 periods after the REM statement in line 10. Oh, by the way, as you probably know, it’s easier and takes less time if you put the computer into the FAST mode before you start entering program lines.
You say you didn’t know that and entered all those periods in the SLOW mode? Sorry about that (ha, ha, ha). Don’t forget to put the computer back in the SLOW mode when you RUN the program.
This program also pokes the entered data into memory but in an area BEFORE the main program locations. That’s the purpose of the periods after the REM statement in line 10. Actually, just about any character will do, but the periods are easy to eyeball, as we will soon see.
RUN the program and ENTER a bunch of names when the prompt is displayed. When you get tired of doing this, just press ENTER for the prompt and a list of what you entered will be displayed. Now LIST the program and observe what’s in line #10.
Pretty nifty, I’d say! To clear the data, ENTER GOTO 2000. When you observe the listing, you’ll see inverse periods in the DATA line #10. This is controlled by the number at the end of line 2030. You can consult the character set chart at the back of the owner’s manual if you want a different character.
I’m sure some enterprising programmer will find this technique useful.
Moving right along, we’ll go on to the next program. SAVE the old and ENTER the new:
10 LET N=1
20 LET A$=""
100 SCROLL
110 PRINT "ENTER ITEM NO. ";N
120 INPUT B$
130 LET A$=A$+B$+","
140 IF B$="" THEN GOTO 200
150 SCROLL
160 LET N=N+1
170 PRINT ,B$
180 SCROLL
190 GOTO 100
200 CLS
210 PRINT AT 12,0;A$
220 SCROLL
230 PRINT "PRESS ENTER TO SEE I
TEMS :::"
240 INPUT B$
250 LET C=1
260 FOR N=1 TO LEN A$
270 IF A$(N)="," THEN GOSUB 300
280 NEXT N
300 SCROLL
320 SCROLL
330 PRINT A$(C TO N-1)
340 IF A$(N TO N+1)=",," THEN G
OTO 400
350 LET C=N+1
360 SCROLL
370 RETURN
400 SCROLL
410 SCROLL
420 PRINT "DONE - PRESS ENTER T
O SEE 1 ITEM"
430 SCROLL
440 INPUT B$
450 CLS
470 PRINT AT 10,0;"WHICH ITEM D
O YOU WANT TO SEE?"
480 INPUT B
485 LET I=0
486 LET C=1
490 FOR N=1 TO LEN A$
500 IF A$(N)="," THEN GOSUB 600
510 NEXT N
520 SCROLL
530 SCROLL
540 PRINT "THERE IS NO ITEM NO.
";B
550 PAUSE 200
560 GOTO 450
600 LET I=I+1
610 LET B$=A$(C TO N-1)
620 IF I=B THEN GOTO 700
630 LET C=N+1
640 RETURN
700 CLS
710 IF B$="" THEN GOTO 520
720 PRINT AT 10,0;"ITEM NO.";B;
" IS"
730 PRINT B$
740 PAUSE 200
750 GOTO 200
RUN this program and ENTER the months of the year for the data items. Just press ENTER for the prompt after all the months are entered. The program now will display the string variable containing the data with the separating commas. You will notice that the string ends with two commas. This is a marker designed to stop search routines later in the program.
If we press ENTER, the individual months will be scrolled on the screen. Not much different so far than what we have seen the other routines do.
But, what if we wanted to single out a single month? Press ENTER and ENTER the number of the month you want to see and that month will be displayed.
This technique was used in the game SCRAMBLED WORDS that appeared on the June/82 cassette where 200 five letter words were held in a data variable and one was picked out at random. A similiar effect can be achieved by changing line #480 to read:
480 LET B=INT (RND*12) +1
The program can be initiated intact by entering GOTO 400. A month picked at random will be displayed on the screen. This is also a useful technique where a number of data string routines can be combined in a single program to produce random sentences such as in the ARTIFICIAL INTELLIGENCE program in the MAY/82 issue.
Perhaps you would like to be able to search through a data string by entering just a portion of that name you are looking for. In the case of our file of months, the month of December is the only one that starts with a <D> but there are three months that start with <J>. <JA> is good for finding January but <JU> will find June if you are looking for July.
In other words, if you want to find a certain data item, you have to type in enough characters unique to the same amount of characters that begin the data item. But what if you are looking for the <A> in April. Since it is the first month that begins with <A>, you would be safe in assuming that you would only need that letter for the search, right?
Wrong! The routine would search for the first <A> in the string and would encounter it in JANUARY and <ANUARY> would be printed. Fortunately, The following routine can be added to the last program and will take care of this problem. Add or change the following lines:
750 CLS
760 PRINT AT 10,0;"SEARCH BY PA
RTIAL NAME ::: ENTER PARTI
AL NAME?"
770 INPUT B$
800 FOR N=1 TO LEN A$-LEN B$+1
810 IF A$(N TO N+LEN B$-1)=B$ T
HEN GOTO 900
820 NEXT N
830 PRINT ,,"PARTIAL NAME NOT I
N FILE ::"
840 PAUSE 200
850 GOTO 200
900 FOR A=N TO LEN A$
910 IF A$(A)="," THEN GOTO 1000
920 NEXT A
1000 LET C$=A$(N TO A-1)
1010 CLS
1020 PRINT AT 10,0;C$
1030 PRINT ,,"IS THIS THE CORREC
T ITEM?"
1040 INPUT D$
1050 IF D$(1)="N" THEN GOTO 1100
1060 GOTO 840
1100 LET N=A
1110 GOTO 820
Voila! When the program cycles to this routine, we are now asked for the partial group of characters that would be pertinent to the data item we wish to display – AND allows a further search if the wrong one was encountered.
Experiment with this routine by entering single letters and two letters for items that start with the same first two characters, such as June and July.
Imagine how useful this would be for finding any data items such as last names in a mailing list file.
These techniques are very memory efficient as compared to dimensionalizing string variables where as much as 50% of memory space may be wasted.
I hope this helps all of you who have been asking about how to simulate READ, DATA and RESTORE on the TS/ZX machines.
Oh, there’s the bell. How quickly these classes go by. Looking at these bottles more closely, I see that they’re not green beer at all. I didn’t know they sold Liquid Plumber in six-packs!
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.