In the last Newsletter we started a D-Base type program; we set up the Menu, Dimensioned all the variables, set up the capabilities to add data and list all the data.
Let’s continue with the next important capability. We can add data and when that data is out of date or no longer required, we need to be able to delete it from our Data Base. If you remember from our last discussion, all the branching menu choices occur at lines of increments of 1000. For example, choice #1 is located at 1000, choice #2 at 2000, etc. The Delete choice, therefore, will begin at line #2000. Add the following lines to your program and they will be explained at the end.
2000 FAST
2010 CLS
2020 PRINT AT 6,0;"ENTER RECORD NUMBER TO DELETE:"
2030 SLOW
2040 INPUT X$
2050 FAST
2060 IF X$<"1" OR X$>STR$ N THEN GOTO 2200
2065 CLS
2066 LET I=VAL X$
2070 GOSUB 20
2080 LET Z=VAL X$
2090 FOR I=Z TO N-1
2100 LET R$(I)=R$(I+1)
2110 NEXT I
2120 LET R$(N)=""
2130 LET N=N-1
2140 PRINT AT 3,0;"RECORD ";X$;" HAS BEEN DELETED"
2150 GOTO 2230
2200 FAST
2210 CLS
2220 PRINT AT 6,2;"RECORD ";X$;" IS NOT ON FILE"
2230 PRINT AT 20,2;"DELETE ANOTHER RECORD (Y/N)"
2240 PAUSE 4E4
2250 IF INKEY$="Y" THEN GOTO 2000
2260 GOTO 100
Lines 2000 to 2030 are self-explanatory. Notice in line 2040 we used x$ instead of an X to wait for a number. This is another kind of “Idiot Proofing”; if we used an X which must have a number entered (no characters whatsoever) then any other character other than a number would have caused the program to stop with an error code. For people, or even for you, if the error code is not handled properly, you would have lost all your data. If this ever happens, always by direct command “GOTO 100” and no data is lost. (That’s why it is always wise to have 1 type of “skeleton” setup so that you can always get back to the Menu without having to remember where the Menu is in each program).
To continue, 2060 is another “Idiot Proof” statement. If your number entered is less than 1 or greater than the records currently on file then the routine jumps to line 2200 to inform you of this. Without this line, the computer would attempt to Delete a file that does not exist. Note also in line 2050; “STR$”. By now, you must realize that every time you see the $ sign, you know we are dealing with strings. Strings can be characters or numbers whereas a letter such as “X” can only represent a numerical value (remember your Algebra days?).
Let’s discuss STR$ and the opposite computer function of VAL. First of all, X$ is used in line 2060 so in order for it to compare whether the following string is larger or smaller, it must be followed by a string. The record number is actually converted for this line only, to a string value. The opposite happens with VAL with one exception; the characters following VAL must be numerical only and will be contained in quotes. Therefore, to sum up, the STR$ function asks the argument following it to be a numerical value to be converted to a string; the VAL will be followed by a string to be converted to a numerical value, e.g. PRINT VAL “1000”. You see this a lot in 1K programs because each numerical value takes up 5 bytes of memory plus a byte for each digit while a string takes up only one byte for each character whether it is a number or a character. Line 2066 converts the value of your Record number which is X$ to a real or a number value, in this case “Z”.
The next little routine is the actual deletion so let’s go through it slowly. (Lines 2066 and 2070 are what’s called in computer programming technology … a stupid mistake. They don’t do anything except confuse the novice programmer which I apologize for). The next line (2090) is a FOR/NEXT loop that starts with the Record you want deleted to the Record number currently on file minus 1. For example, if we have 10 records on file and we want to delete Record 5, then the loop starts off with 5 to N (record number)-1 or, in this case, to Record 9. Since we are deleting 1 record, there will only be 9 records instead of 10. Line 2100 does the exchange. To continue with the example, 2100 says R$(5) will now equal R$(5+1) or R$(5) will equal R$(6). Follow the logic carefully – whatever is on the left side of the “=” sign will not be whatever is on the right side of the “=” sign. If R$(5) or Record #5 was Mr. Jones and R$(6) was Mr. Smith, R$(5) would now be Mr. Smith and the record with Mr. Jones would no longer exist. If the computer was stopped right now, record #6 would also be Mr. Smith. But the loop increases by 1 and R$(6) would contain the string or record of R$(7) and so on up to the end of our example of 9 where Record #9 would now contain whatever was in R$(10). Remember, N-1- Again, remember that R$(9) has now become whatever was in R$(10) so that R$(9) and R$(10) have the same data; hence line 2120 which declares R$(N) which is the current number of records on file or, in our example, the 10th record becomes an empty string. Two quotations following each other with no spaces or characters means an empty string. Line 2130 now makes sure that since we have deleted a file, we can make use of the empty file so it subtracts 1 from the current record number; in our example again, we had 10 records, deleted 1 record so 1 is substracted. Currently N=10 and N-1 means N=9. Line 2140 advises the user, his selected record number has been deleted then goes on to ask if you want any more records deleted.
If you wish to “Idiot Proof” a bit more, you can use the previous mentioned “stupid mistake” lines. By adding one more line such as: 2075 PRINT “IS THIS THE RECORD YOU WANT DELETED (Y/N)?” and the lines that must follow follows through with the deletion if “Y” or goes back to the Menu if “N”. This is in case you entered the wrong number by mistake. You should be able to add those lines yourself by now so give it a shot; the computer won’t blow up if you’re wrong. The remaining lines for the deletion section are self explanatory.
In order to SAVE the program with data, we enter choice #6 which goes to line 6000. Add the following lines to your program; (start your tape to SAVE then press “6”).
6000 FAST
6010 CLS
6020 SAVE "NAME FILE"
6030 GOTO 100
Just a short explanation of these lines. First of all, any Basic program you list, do you ever wonder why the last character of the SAVE name is inverted? This is used by the machine code which helps in its speed and recognition. It reads each character until it sees the inverse character and “knows” that it is the end of the SAVE name. Upon LOADing at a different time, it jumps to the Menu keeping all the data and variables intact. If 6030 has said RUN, then this command resets all variables to nothing (does not exist) and everything must be redimensioned and there obviously would be no data on file. (CLEAR does the same thing). This is why suggestions are made to “CLEAR” the variables (if they are of no value) prior to SAVEing a program. In our program, if all variables are CLEARed, the program would SAVE in 2 to 3 minutes while otherwise it could take up to 7 minutes to SAVE and LOAD. Of course if it was CLEARed prior to SAVEing, we would lose all our data which we don’t want.
Well that’s is for this Newsletter in programming in Basic. If you need further explaining, let me know or if you need answers or answers to specific questions about Basic programming, again let me know or any other of our more experienced members which include most of the executive officers.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.