THE BEGINNER'S PAGE
Richard Mansfield, Senior Editor
Your First Useful Program
It doesn't take long. Soon after you buy a computer, someone will ask what it's doing for you that's useful. You've been learning to program in BASIC, but what practical results have you got to show for your efforts? The questioner might be a friend or even someone in your immediate family, someone you usually love.
Here's a program which can be used in dozens of practical ways. It makes it easy to store and analyze information. We all keep lists and records. If you have a collection of something – recipes, books, stamps, albums, whatever – you can enter all sorts of information into this program and then look things up later in a variety of ways. You can ask for everything starting with the letter A, everything on a particular topic, from a particular country, or whatever specifications you want. This type of program is often called a data base manager. It's one of the most effective, impressive applications for a personal computer.
How To Modify The Program
A similar program was published in this column two years ago and proved popular. I received this letter the other day:
I would like to ask a favor. I have been using your program "Searching Files" (November 1981) for some time, and very much appreciate it. But I've tried without success to add categories like 3. Publication, 4. Date. If possible, could you explain how to expand the program to include additional categories?
Mel Leiserowitz
That a programmer can make modifications to a program is one of the most subtle, but powerful, aspects of computing. Unlike other kinds of tools, a given computer program can often be radically transformed so that it can accomplish a great variety of tasks. Let's take this program apart, looking at each aspect of it, and then explore how to modify it to include extra categories.
Data base management is frequently divided into two phases: the manager program and the actual base of data. A data base would be a list of pieces of information, perhaps in alphabetic or some other order, like the phone book. The data base is often stored on a tape or disk, and the manager program looks up information by opening and closing files on the tape or disk. The second phase, the manager program, can make all kinds of lists for you; it can analyze the data base. For example, if the yellow pages were on a disk, you could write a program which would allow you to ask highly specific questions. You might want to know the phone number of all pizza shops within a five-mile radius of your house. If the data base included map information, the manager program could give you the answer.
A Faster And Easier Way
This program, "The Filer," combines the data base into the manager program in the form of DATA statements, each holding an individual record. The advantage of this is that you can add to and modify the data easily, on screen. It's also faster: the computer doesn't need to bring data in from tape or disk. The program contains the data already. The disadvantage is that your data base cannot be larger than the amount of memory in your computer. You should be able to find many uses for this program, however. If your computer has 32K of RAM memory, you can store detailed information about all the articles in COMPUTE! for any given year. If you want to start a data base for a new year, you can simply use the program again, with a new set of DATA statements. In our example use of The Filer we'll start a cross-indexed reference file of all COMPUTE! articles.
The program is designed to work on all computers using Microsoft BASIC (Commodore, Radio Shack, TI, Apple, etc.). If you have an Atari, there's a version for your machine in COMPUTE!, November 1981.
Let's go through the program to see how it works:
Line Number
1 This lets the computer know the total number of items in our data base. T = 10 because we've got ten DATA lines in this program. The data base starts at line 502 and continues to the end of the program. If you add 400 more DATA lines, you should change line 1 to read: T = 410.
2 Since reading a large data base might take some time, this statement appears on screen to let the user know that the computer is busy and will return control to him or her shortly.
3 Here we DIMension the three variables which will be holding our data. These tens, too, would need to be changed to 410s if you added 400 more DATA lines.
10 The computer assigns a special variable name to each item of data by READing through the entire list. We've got three categories per record. A$(? - whatever I = during the READing) will be topic identification for the COMPUTE! articles in our data base. B$(?) will contain the issue number and the page number. C$(?) holds the author name.
15-45 Here the computer gives us a choice. We can look things up either by topic or by author.
50 We now make our request. If X = 1 (see line 35), then we're after the author so we're sent down to line 70, which searches through C$(). If not, we proceed to line 55 for a search of A$(), topics. Lines 55-65 and 70-80 are identical searches, except one looks at C$(), the other at A$(). Since they're the same, we'll just examine the interesting pattern-matching technique where it appears the first time, in line 60.
60 This is the heart of the program. It's the trick that lets you look things up without knowing their exact names. It also makes possible varying depths of specificity. If you add a data line: 522 DATABASIC MEMORY SAVING, you can then request anything from B to BASIC MEMORY SAVING and this item will show up on the list. If you request matches to BASIC, you'll get this one and line 510. If you request BASIC MEMORY, line 510 will be ignored.
How does it work? N$ is the word or words you entered (line 50) and to which you want all matches. A$(I) will scan through the entire data base "topics column." For a match to take place, only the leftmost part of A$(I) needs to match N$. Z does this for us; it's the length of N$ (see line 50). That is, we're looking for matches from pieces of A$(I) only as big as N$. So, you type in your topic DATA with this in mind. Enter each record so that the first word is the most general, the second more specific, etc.
85 This subroutine prints out any matches which are found. It contains descriptions of the categories. There are a number of ways to format such things. You might prefer, for example, to list the category titles only once, at the top of the screen, and then list everything in columns underneath them.
Expanding The Categories
As you can see by following the changes suggested in Programs 2-4, some minor structural modifications are necessary to make Program 1 handle a fourth category, Computer brand. However, it would be simple to add a fifth or more categories to this new version.
One thing to watch out for: the READ statement doesn't care what data is on a given DATA line. It reads things very literally and checks for commas (or the end of a line) to tell it that a particular item has ended. So, if you get odd responses such as EDUCATION when you're asking for author names starting with E, or an OUT OF DATA ERROR – you've probably left out a comma somewhere in the DATA lines. Also, your DATA lines will be longer with this new, four-category, version of The Filer. They'll now look something like this:
520 DATALANGUAGES PILOT,1/40,THORNBURG,VIC
If you have any questions or topics you'd like to see covered in this column, write to "The Beginner's Page," COMPUTE! Magazine, P.O. Box 5406, Greensboro, NC 27403.
Program 1: The Filer
1 T = 10 : REM{8 SPACES} TOTAL NUMBER OF ITEMS OF DATA 2 PRINT"{3 SPACES}READING{5 SPACES}COMPUTE!{5 SPACES}DATABASE 3 DIMA$(10), B$(10), C$(10) 10 FOR I = 1 TOT : READ A$(I), B$(I), C$(I) : NEXT 15 PRINT : PRINT"SELECT A CATEGORY" : T$ = "TOPIC" 20 PRINT"{4 SPACES}1. AUTHOR 25 PRINT"{4 SPACES}2. SUBJECT 30 K$ = "" : GETK$ : IFK$ = "" THEN 30 35 X = VAL,(K$) : IF X > 2 THEN 30 40 IF X = 1 THEN T$ = "AUTHOR'S NAME" 45 PRINT"{3 SPACES}PLEASE ENTER "T$ 50 INPUT N$ : Z = LEN(N$) : IF X = 1 THEN 70 55 FOR I = 1 TO T 60 IF N$ = LEFT$(A$(I), Z) THEN GOSUB 85 65 NEXT I : GOTO 15 70 FOR I = 1 TO T 75 IF N$ = LEFT$(C$(I), Z) THEN GOSUB 85 80 NEXT I : GOTO 15 85 PRINT A$(I) ; " … IN ";B$(I) ; " (ISSUE#/PAGE), WRITTEN BY ";C$ (I) : RETURN 498 REM 499 REM 500 REM *** TOPIC OF ARTICLE -- ISSUE/PG -- AUTHOR *** 502 DATABUSINESS, 1/4, SAWYER 504 DATASORTING, 1/7, HULON 506 DATAWORDPROCESSORS, 1/13, LINDSAY 508 DATASCIENTIFIC INSTRUMENTATION, 1/24, BYRD 510 DATABASIC TOKENS, 1/29, HERMAN 512 DATAMODEM, 1/30, TULLOCH 514 DATAA-D CONVERTER, 1/31, HERMAN 516 DATAML MEMORY TEST, 1/32, MOSER 518 DATAEDUCATION, 1/34, BARRETTE 520 DATALANGUAGES PILOT, 1/40, THORNBURG
Program 2: Change These Lines
3 DIMA$(10), B$(10), C$(10), D$(10) 10 FORI = 1 TO T : READA$(I), B$ (I), C$ (I), D$(I) : NEXT 35 X = VAL (K$) : IF X>3 THEN 30 50 INPUTN$ : Z = LEN (N$) 55 FORI = 1 TO T : ONXGOSUB 60, 65, 70 : NEXT I : GOTO 15 60 IFN$ = LEFT$ (C$(I), Z) THENGOSUB 85 65 IFN$ = LEFT$ (A$(I), Z) THENGOSUB 85 70 IFN$ = LEFT$ (D$(I), Z) THENGOSUB 85
Program 3 : Add These Lines
26 PRINT "{4 SPACES}3. COMPUTER 42 IFX = 3 THENT $ = "COMPUTER" 61 RETURN 66 RETURN 71 RETURN
Program 4 : Drop These Lines
75 IFN$ = LEFT$ (C$ (I), Z) THENGOSUB 85 80 NEXT I : GOTO15