PROGRAMMING THE TI
Part 2: File Processing
I have had lots of requests for programs that use TI Extended BASIC and peripherals (printers, the RS-232 interface, and disk drives). In these next two columns I will try to satisfy those readers, plus the readers who have requested file processing, by discussing a report writer that uses disk files and a printer.
Keep in mind that there are many ways to program (as many ways as there are programmers). I'm going to show you one method I use to set up a file, then generate several reports from that file. For an example, I'm going to use a hypothetical situation—a teacher has divided the class into three reading groups. The students are given the goal of presenting one oral book report each week of the term (for this example the term is ten weeks). The teacher will grade the students on how well the goal is attained. These reports keep a tally of the book presentations. As part of the necessary school paperwork, the teacher also needs to keep track of each student's address and phone number. As a personal touch, the teacher also wants to be able to acknowledge the students on their birthdays. We'll get to these last two programs next month.
Writing A Data File
First, a data file is set up using Program 1. The student information is listed in DATA statements within this program, and the program will generate a data file on diskette. You could use DATA statements within a regular program which also writes the report, but with many students a shortage of computer memory could be a problem. Also, I'm going to use this data for several programs, and I'll only have to type it once—in this data file generator program. All reports will use this one data file.
Program 1 READs the information in from DATA statements, then writes it directly out on a disk file. Line 130 is the key statement to set up the disk system for storing the data. The OPEN statement says device #3 (you can use any number) is disk drive number one, and the file of data will be called SAMPLE. The data will be stored in internal format as output and can have a variable length of 192.
To use this program with cassette instead of diskette, use this line:
130 OPEN #3:"CS1", INTERNAL, OUTPUT, FIXED 192
In later programs change any DSK1 and titles to CS1 within quotes, and change the VARIABLE to FIXED since cassettes can handle only FIXED-length files.
Reading The Data
Line 140 reads the data in the following order for our situation: group G, last name N$, first name F$, address A$, phone number P$, birthday expressed as a number BD, report R$, and comments C$. Line 150 prints the same information onto the diskette. Lines 160–180 stop the process if the name read is "ZZZ", which indicates the end of the file. Line 170 counts the names.
Lines 190–90 contain the data. Notice that the last DATA statement contains "ZZZ" for N$, the last name. These names are sample names only and are not meant to represent any real people. The other information is also made up for purposes of illustration. The DATA statements are in order by last name.
The first number in the DATA statement is a group number—the sample class is divided into three reading groups. We're assuming the students all live in the same city, so only the street address is stored as A$. The phone number P$ is stored as a four-digit number because they all have the same prefix. You can change this if you wish. Since the variable name is P$, you may include a hyphen in the phone number. The birthday BD is expressed as a number which consists of the month number then two digits representing the day number. For example, September 24 is month 9 and day 24 for 924. November 25 is 1125. October 5 is 1005 for month 10 and day 05.
Encode The Special Cases
The next series of numbers represents whether the student presented a book report or not. I combined all the weeks into one ten-digit number. A 1 means the student gave a report that week and 0 means he or she did not. A dash means the student was not enrolled. You may have a report that uses other symbols for other purposes—an asterisk for a different assignment, for example. The comments C$ are just to illustrate more versatility in later reports. I used AUDIT to represent a student who will not be counted for credit, and MOVED indicates the student is no longer in the class.
To try the sample programs this month, start with a new initialized diskette. Type in Program 1 then save it with a command such as SAVE DSK1.LIST1 or SAVE DSK1. NAMES or whatever label you wish. Next RUN this program with the diskette in the disk drive. The drive light will blink on and off as data is being recorded. The screen shows how many names have been processed. This program only sets up the data files—it does not write any reports.
Generating A Roster
Now go on to Program 2. This program will read information from the data file we created previously with Program 1 and print out a roster of information—the student, which group the student is in, the address, the phone, the birthdate, and any comments.
Lines 130–150 contain a procedure that reads the month names, which are later used in printing the birthdays. Line 160 initializes some variables. Note that Extended BASIC allows several variables at once to be set equal to zero. The variable I is used to count the total number of names. N is the number of auditing students, and MM is the number of students who have moved. L is a line counter.
My printer has a feature that will skip over perforations at the end of a page. In case your printer does not automatically do that, I have included this method. L is used to count the lines that have been printed. Line 360 can then check to see if it is time to change pages. PRINTing CHR$(12) goes to the top of a new page. If your printer cannot PRINT CHR$(12), you can put in a pause so you can reset to the top of a new page. (The sample data included here does not go more than one page. Add more names if you want to see this part work.)
Line 180 defines the printer configuration for device #2 (use any device number). Use the parameters you need for your particular printer. Consult the RS-232 manual and your printer manual to figure out your configuration. Line 190 is the OPEN statement for device #3 (any number) to read in the data. Notice that this statement matches the OPEN #3 statement in Program 1, except we use INPUT instead of OUTPUT because we will be reading in data.
Lines 200–240 print the heading. Line 250 is an INPUT #3 statement that tells the computer to READ data from device #3. This statement is just like using READ and DATA statements within the program, only we use data files instead of program statements to store the data. The items listed may be read in as needed or all on one line, but must be in the same order as we previously saved them.
Ordering The Data
Line 260 skips the name on the roster if the student has moved and counts the number of people who have moved. Line 270 skips a line when the name starts with a different letter of the alphabet. By listing the original data in alphabetical order, this roster and other reports will automatically be in alphabetical order. A blank line is printed between groups of names starting with different letters. Line 280 checks for the last data item.
Line 290 combines the last and first names. Lines 300–310 determine the birthday from the number BD. Line 330 combines the common prefix 586- with the data P$ for the phone number.
Lines 320 and 410 are IMAGE statements, a feature of Extended BASIC which makes the module worth its price if you do lots of reports. An IMAGE statement allows you to specify how a line will be printed.
You can also use IMAGE statements PRINT USING statements to line up columns of money. For example, $###.## will print a number in dollar format with the cents rounded off. Line 340 uses line 320 to print the information.
Lines 350 and 360 increment the number of names and number of lines printed. Line 370 checks for a student who is just auditing the class. Line 380 causes the computer to branch back to the INPUT statement to read the next data items.
If you get a data error, the most likely cause would be mistyping the DATA statements in Program 1. When the program stops with an error, you can PRINT N$ to see which was the last name that was accepted. Use that information to try to pinpoint a typing error in Program 1. (Be sure to SAVE Program 2 before you go back to Program 1.) You also need to make sure the data items in line 250 of Program 2 are in the same order as the data items in line 150 of Program 1. You may use different variable names if you wish, as long as the strings and numeric variables are in the right order.
Lines 390–160 print the totals information. Line 470 uses CHR$(12) to tell the printer to go to a new page. Line 480 CLOSEs both devices used in the program.
Program 1: Write Data Files
80 REM TI EXTENDED BASIC 90 REM DISK, PRINTER