“MSX BASIC” COLUMN by Orazio Cacciola (part 12)
THE MATRIXES. THE DIM/ERASE COMMANDS and THE CLEAR/FRE FUNCTIONS
It is very inconvenient when we have to make a program that makes use of many variables that will be used several times. Think of a sequence of constants that are called from different points in the program or an archive with names, addresses, etc. Let's take the first mentioned topic, constants, as an example, and let's pretend that our program will use 20 of them, so we will have to create them. Since these variables will have the same functionality, to create them we could use a variable that we will call C, followed by a number. For example:
C1,C2, C3, C4, and so on. However, we know that MSX BASIC only stores the first 2 characters of the variables, therefore since the variables must be 20, using the method described above allows us to use up to the variable C9 because the C10 will be 'seen' by Basic as C1 and it will store the data on the last one, deleting the previous one. So to continue we have to use another letter. Imagine what a mess. But fortunately there is a solution to all this. This is called ARRAY OR MATRIX. Arrays are almost always created at the beginning of the program, they reserve drawers in the memory where all the data will be stored. We say how much of these assets we want to reserve, using the DIM command. Let's take the example above: DIM C(19). I put 19 because MSX BASIC starts counting from 0. This also applies to alphanumeric variables. To continue, already knowing the data, since it is constant, we can load it into a DATA function and recall it with a FOR/NEXT loop.
I'll make a small example program.
10 DIM C(19) 20 FOR I=0 TO 19:READ C 30 C(I)=C: NEXT I 40 DATA ..........
We can also create 2-dimensional, three-dimensional and even larger matrices, but given the limited memory of our MSX it will be prohibitive to use matrices that are too large, the memory would run out very quickly. To create a 2-dimensional matrix just add a second data followed by a comma: DIM C(19.3), by doing so we have reserved 80 (20X4) drawers in memory. While for a three-dimensional one, it will be: DIM C(19,3,2). This time there will be 240 (20x4x3). The memory in bytes that the so-called drawers occupy (this is my way of calling them) will vary depending on the variable stored. If we need to use a matrix that does not exceed 10, i.e. eleven insertions, it is not mandatory to initialize it with the DIM command, even if it is two-dimensional or higher. We cannot dimension a variable twice, if this happens it will be reported as an error, unless it has been eliminated with the command that I will explain below.
After creating a matrix and using it up to a certain point in our program, we know that from now on it will no longer be needed or some data will no longer be useful. We can delete it, freeing up memory space, using the ERASE command. The latter will be deleted together with all its data. In the case of the second argument mentioned above, we will resize the variable with the correct parameters and re-enter the data we will use. After the ERASE command you will only have to enter the name of the variable without its sizing. The correct syntax is: ERASE Variable,[Variable],[Variable] ……
CLEAR
Clears all variables, even those in arrays, closes files and eliminates functions created with DEF. Change the space reserved for strings, by default it is 200 (characters). Sets the maximum address that can be used by Basic MSX. It can be used without arguments to clean up memory.
This function tells us how much free memory is available in Basic. We can also know the free space reserved for strings. To find out how much free memory we still have available, just add a number inside the round brackets: PRINT FRE(0), for strings 2 quotes are enough: PRINT FRE(“”). The function arguments are fictitious, the important thing is to use a number for the first definition and a string for the second. If we want, we can also insert it into a variable: A = FRE(0)
Syntax: FRE(0) or FRE(“”)
10 CLEAR(2000):DEFINT AZ: SCREEN 0: DIM M$(50,2):CN=0 20 CLS:LOCATE 15,1: PRINT "MENU": GOSUB 100 30 S$=INKEY$: IF S$="" THEN 30 40 IF S$<"1" OR S$>"4" THEN 30 50 ON VAL(S$) GOSUB 200,300,400,500 60 GOTO 20 100 LOCATE 1,4 : PRINT"1 - DATA ENTRY" 110 LOCATE 1,8 : PRINT"2 - MODIFICATION DATA" 120 LOCATE 1,12: PRINT"3 - VIEW DATA" 130 LOCATE 1,16: PRINT"4 - PROGRAM OUTPUT" 140 RETURN 200 IF FRE("")<50 OR CN=50 THEN PRINT "ATTENTION !! Not multiple insertions can be made": FOR R=0 TO 2000:NEXT R: RETURN ELSE CN=CN+1 205 CLS: LOCATE 12,0: PRINT "INSERT" 210 LOCATE 1,4: LOCATE 1,4:INPUT "NAME ";M$(CN,0) 220 LOCATE 1,8: INPUT "ADDRESS";M$(CN,1) 230 LOCATE 1,12:INPUT "TELEPHONE"; M$(CN,2) 240 LOCATE 1,18: PRINT "CORRECT (Y/N)?" 250 S$=INKEY$: IF S$="" THEN 250 260 IF S$="s" OR S$="S" THEN RETURN 270 IF S$="N" OR S$="N" THEN 205 ELSE 250 300 CLS:LOCATE 15, 0: PRINT "EDIT" 303 INPUT "INSERT THE NAME TO SEARCH";N$ 306 FOR I=1 TO CN:IF N$=M$(I,0) THEN 310 308 NEXT: PRINT "Name not found":FOR R=0 TO 2000:NEXT: RETURN 310 LOCATE 0,4:INPUT "NAME(Confirm to not change)";N1$ 320 IF N1$="" THEN LOCATE 1,29:PRINT M$(I,0) ELSE M$(I,0) =N1$330 LOCATE 0,8: INPUT "ADDRESS(Confirm to not change)";I$ 340 IF I$="" THEN LOCATE 0,9:PRINT M$(I,1) ELSE M$(I,1)=I$ 350 T$= M$(I,2):LOCATE 0,12:INPUT "PHONE(Confirm to not change)";T$ 360 IF T$="" THEN LOCATE 0,13:PRINT M$(I,2) ELSE M$(I,2) =T$ 370 CLS:RETURN 400 CLS:LOCATE 10,0: PRINT "DISPLAY" 410 FOR I=1 TO CN 420 PRINT "NAME:"M$(I,0);TAB(20);"ADDRESS.";M$( I,1):PRINT"PHONE:";M$(I,2) 430 IF IMOD10=0 THEN GOSUB 450 440 NEXT: GOSUB 450: RETURN 450 PRINT:PRINT "Press space to continue" 460 IF INKEY$=" " THEN CLS : RETURN ELSE 460 500 CLS:ENDLine 200: I have a check done in the Insertions subroutine to see if there is still space in the string memory with the FRE function or if it has reached 50 insertions.
In the rest of the program, replace all normal variables with matrices. For research, since I haven't yet talked about the functions that work with strings, I left almost everything as it was. But it will be revived in due course.
Another big flaw of this program is that when we close it, all data will be lost. But there is a remedy for this too which I will explain when I touch on the subject of FILES.



0 Comments