
Good morning, Class! Today’s session will involve the techniques of compacting data or, in other words, putting data into the least amount of memory space possible. Consider a situation where you want to enter data into the computer for a customer mailing list.
You would want to enter the customer’s company name, the customer’s name, the company address, the town or city, the state, the zip-code, maybe the phone number and possibly a remarks entry.
A string variable would have to be assigned to each of these entry items and dimesionalized accordingly.
On the TS-1000, care has to be taken to dimensionalize each variable to allow the maximum amount of characters for that input. Let us just look at names. Let us say that the smallest company name is the “Al CO.” which occupies 6 total memory or character spaces. However, the largest name is “UNITED AMALGAMATED INDUSTRIES” which occupies 29 character spaces. Let us say that the average amount of character spaces used by any of these name entries is 13 characters.
If we had 100 customers and we wanted to allow “UNITED …” to be entered in full, we would have to format our dimension statement as DIM N$(100,29). Any amount less than 29 would chop off that many characters from the left of “UNITED …”.
The same holds true for any of the other variables. When “Al CO.” is entered, 23 memory spaces are entered. Since the average entry occupies 13 characters, 1,600 memory spaces are wasted. Eight other variables are dimesionalized for data entry. Let us say that each one wastes an average of six memory spaces (8 X 6 X 100). We have 4800 bytes along with the original 1600 for a total of 6400 wasted bytes.
This, of course, is a goodly sum. If we are smart enough (and sneaky), we can tone this down to where there is only one wasted byte per variable. Enter the following program.
10 PRINT "NAME?"
20 INPUT N$
30 PRINT "ADDRESS?"
40 INPUT A$
50 PRINT "CITY/TOWN?"
60 INPUT C$
70 PRINT "STATE?"
80 INPUT S$
90 PRINT "ZIP CODE?"
100 INPUT Z$
200 LET B$=N$ + A$ + C$ + S$ + Z$
300 PRINT B$
If we RUN this program, let us enter the following data for purposes of example:
"JOHN JONES","123 45TH ST",
"ANYTOWN","IL","66666"
You will notice that after the last entry, the screen will display:
JOHN JONES123 45TH STANYTOWNIL66
666
This, of course, is the most compact format we can get and uses only 35 bytes of memory. Unfortunately, there is no technique to separate the individual data items.
All is not, however, lost! Separators can be inserted in between the variables which can be used to break the main variable apart. Change line 200 to read:
200 LET B$=N$ + "*" + A$ + "*"
+ C$ + "*" + S$ + "*" + Z$ + "*"
If we execute the program again (use GOTO 200 rather than entering the data from scratch), the following is displayed:
JOHN JONES*123 45TH ST*ANYTOWN*I
L*66666*
This uses up a total of 40 bytes, five more or one extra byte for each of the five variables. Much more efficient than the 6 average wasted bytes we discussed earlier. It isn’t hard to calculate that with a nine entry/100 customer program, we would only be wasting 900 bytes as opposed to the 6400 utilizing the old method – a savings of over 5K of RAM memory. This could double the amount of customers to be entered into your file in a 16K machine.
Now we have to have a method of breaking the variable “B$” apart to display the separate entries. Easier done than said. Delete line 300 and enter the following lines:
210 CLS
220 LET Q=1
230 FOR N=1 TO 5
240 GOSUB1000
250 NEXT N
260 STOP
1000 FOR I=Q TO LEN B$
1010 IF B$(I)="*" THEN GOTO 2000
1020 NEXT I
2000 PRINT B$(Q TO I-1)
2010 LET Q=I+1
2020 RETURN
The variables are now all neatly separated from the main variable B$. The gentleman in the rear has his hand up! Yes, Sir?
He says that we have been shown how to separate undimensionalized variables and wants to know how to do it with variables that are dimensionalized.
First of all, let me say that asterisks (“*”) are not the best devices to use as separators. We may have an entry that has an asterisk as part of its content. Or, we may have a situation where we need more than one type of separator, particularly if a fast search is necessary.
There are many characters in the Sinclair Code library that are not accessible directly from the keyboard except through the CHR$ function. Examine the CHARACTER SET table in your manual and you will find 50 of these unused codes, from 67 to 111 and 122 to 125. This does not mean that you cannot use them. Line 200 could be changed from:
200 LET B$=N$ + "*" + A$ + "*"
+ C$ + "*" + S$ + "*" + Z$ + "*"
to read:
200 LET B$=N$ + CHR$(67) + A$ +
CHR$(68) + C$ + CHR$(69) +
S$ +CHR$(70) + Z$ + CHR$(111)
A routine could be written to identify that the characters between the code characters 67 and 67 represent the address.
The code numbers are imaterial so long as they are classified as unused. In answer to the gentleman’s question, consult the August/83 issue, page 4 and read the article about the “MINI DATABASE” program. Look at the listing. This program needs no dimensionalizing and creates a single string variable that it keeps tacking data onto. This happens between lines 1000 and 1430. Lines 4000 to 4830 allow a keyword search that will allow any set of data items to be located and extracted. It is weak as far as data base programs go but it sure is efficient when it comes to memory conservation. Routines could be added to segment the main string variable into even sections. This would use memory space, both for the empty spaces in the unused portions of the segments and for any extra routines to be written such as a sort routine. However this way, a sort routine could sort all the data items while they were still contained in the variable. The main variable would not have to be dimensionalized and some memory would still be saved.
A good homework exercise – Class dismissed!
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.