A long time ago, in a secret factory far, far away, the Radio Shack TRS-80 Model I was produced and sold to beginning programmers as their first home computer. The Trash-80 was not exceptionally fast. If memory serves me, processing speed was 25,000 MHz. Hard Disk drives (called Winchester drives) were very expensive and not easily afforded by the average hobbyist. Floppy drives were equally expensive, and used 5.25 inch floppy disks. The term floppy remains with us even after physical construction became a 3.5 inch diskette. Because the hobbyist needed a way to store programs and data written at home, a common cassette player served the purpose of saving data to cassette tape. Cassette players were abysmally slow, but they worked!
Like all home computers of that day, the Trash-80 came with only 4K of memory. At the time, the practical limit for home computers was 64K, and RAM was very expensive! Hobby programmers had to be very creative when writing their home programs. It would not be until many years later that the 64K barrier was broken, and the current amount of RAM in your PC is limited only by your pocket book.
NOTE: Save each of these snippets to your project folder. Don't run Snippet#3 first, just to get the answer to your question! Run each snippet in sequence and you will learn how easy it is to complete a task, as well as build your program in a modular fashion to accomplish more than one task.
Now... let's see what a hobbyist might do if required to work with a large data file, and we're going to pretend we have a maximum of 4K RAM in our PC. First, create a folder in your JB root folder; call it Krazy Karls Used Kars, or something else if you like. Save this snippet to your folder and run it one time only! This snippet will create a data file for the year 2000, and save 1,000 total sales by 6 employees of Krazy Karls business. You can recreate this data file anytime you like, but if you are comparing dates, sales persons, or prices, the results of subsequent runs will give different answers. Snippet for dummy data file
'A program to generate dummy data file for Krazy Karl's Used Kar business.
firstDay=36159 'Serial number of Jan 1, 2000
month$="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
salesPerson$="John Mary Stanley Henry Phyllis Damion"
price$="1295 1785 2275 999 2995 1895 3199 4289 1495 2495"
OPEN "salesData.txt" FOR OUTPUT AS #1 'ERASES previous salesData.txt!!!
FOR k=1 to 1000
soldDate=(INT(RND(0)*365)+1)+36158 '36159 is first day of 2000
person=INT(RND(0)*6)+1 'Generate random number for salesPerson
person$=WORD$(salesPerson$,person) 'Get name from salesPerson$
amount=INT(RND(0)*10)+1 'Generate random number for amount
sale=VAL(WORD$(price$, amount)) 'Get amount from price$
PRINT #1, soldDate;", "; person$;", ";sale 'Print data to dummy file
NEXT k
CLOSE #1
PRINT "Program complete."
END
Now that we have a data file to work with, let's find out the total sales of Karl's business. But my salesData.txt has 19.9kb used for data! How can I ever load that into only 4K of RAM? Easy... I won't! These snippets will not use an array to work with the data, we certainly cannot put 19.9kb of data into arrays on a computer with 4kb of RAM! We'll use an easier trick... accumulators! We'll read the data file and add the price of every car sold to an accumulator in the program, and then display the total sales for the year. Snippet#1
'Annual sales of Krazy Karl's for year 2000
OPEN "salesData.txt" FOR INPUT AS #1
WHILE EOF(#1)=0
INPUT #1, dummy, dummy$, price
revenue=revenue+price 'Total sales equal revenue+each price
WEND
CLOSE #1
PRINT "$"; USING("#######",revenue)
END
OMG! Is it that simple?
Yes, it is! revenue is an accumulator. With each pass through the WHILE/WEND loop, the price of each car sold is added to the accumulator. The result is total sales revenue for year 2000. Our snippet required 8 lines of code and only 237 bytes of RAM.
But Karl wants to compare the sales for each of his 6 employees. Okay. We'll build on the code we've just written. Snippet#2
'Find total sales for an individual
PRINT "Damion Henry John Mary Phyllis Stanley"
INPUT "Which sales person to calculate? "; person$
OPEN "salesData.txt" FOR INPUT AS #1
WHILE EOF(#1)=0
INPUT #1, dummy, salesPerson$, price
IF salesPerson$=person$ THEN
pay=(pay+price) 'Accumulate pay for this person
END IF
WEND
CLOSE #1
pay=pay*.10 'Commission is 10% of total sales
PRINT "$"; USING("#######",pay)
END
Snippet#2 reads every line of data in the file. IF person$=salesPerson$ THEN pay=pay+price; if person$<>salesPerson$, then the data is ignored and we read the next line in the file. Isn't that easy? Our snippet requires only 13 lines of code and 399 bytes of RAM.
Krazy Karl decides he can boost sales aggressiveness if he gives the top-selling sales person a bonus of $100 for each month. How can he use this extremely simple code to determine who the top-sales person is? Again, it's very easy! Snippet#3
'Find total monthly sales for an individual
PRINT "JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC"
INPUT "Sales month to search for? (MMM): "; month$
PRINT "Damion Henry John Mary Phyllis Stanley"
INPUT "Which sales person to calculate? "; person$
OPEN "salesData.txt" FOR INPUT AS #1
WHILE EOF(#1)=0
INPUT #1, salesMonth, salesPerson$, price
SELECT CASE month$
CASE "JAN" : first=36159 : last=36190
CASE "FEB" : first=36190 : last=36219
CASE "MAR" : first=36219 : last=36250
CASE "APR" : first=36250 : last=36280
CASE "MAY" : first=36280 : last=36311
CASE "JUN" : first=36311 : last=36341
CASE "JUL" : first=36341 : last=36372
CASE "AUG" : first=36372 : last=36403
CASE "SEP" : first=36403 : last=36433
CASE "OCT" : first=36433 : last=36464
CASE "NOV" : first=36464 : last=36494
CASE "DEC" : first=36494 : last=36525
END SELECT
IF salesMonth>=first AND salesMonth<last THEN
IF salesPerson$=person$ THEN
pay=(pay+price) 'Accumulate pay for this person
END IF
END IF
WEND
CLOSE #1
pay=pay*.10 'Commission is 10% of total sales
PRINT "Sales by ";person$;" for month of ";month$;_
" equal $"; USING("#######",pay)
END
To begin, we have to choose which month we want to report. We need to know if the salesMonth$ falls within a range of dates we're seeking. DATE$() returns a string value from the computer. We can't determine if something is greater than or less than if using a string. So I determined the serial number for the first and last day of each month during 2000, using the DATE$("mm/dd/yyyy") function of Just Basic. Then I used a SELECT CASE in order to assign a value for first and last day of the month in question.
Once we have established parameters, we can read every line of data in the file. If salesMonth falls within the limits of first and last, if the salesPerson$=person$ we are looking for, then the pay for that person equals pay=(pay+price)*.10, each sales person receives a 10% commission of the total sales price.
Our completed program finds the total sales revenue for a full year, identified by month, and further identified by the sales person. The data file requires 19.9kb, but the completed program requires only 1.11kb. At no time is more than 1.11kb of RAM used to run this program, plus a small amount of overhead for the accumulator values with each run.
Fast forwarding to today's date, the home PC has grown exponentially in speed, RAM, and ability. You can create and work with arrays requiring many mega-bytes of RAM, or you can work with accumulators and no RAM. New technology should be used to expand your programming abilities too, but if you fall behind, some of the old techniques may be just what you need!