Printing Databases

See all articles from Sinc-Link v11 n2
Author(s): Peter Hale
Publication: Sinc-Link
Pub Details: Sinc-Link v11 n2 (Volume 11, Issue 2)
Date: March/April 1993
Pages: 17-18

Computers are nice for storing data, but often it is tedious to recover that data in an economical and useful format. This is particularly true of printed hardcopy.

Those of us raised on print medium are more comfortable with text on paper, and certainly there are few who are comfortable taking the computer into the bathroom. Perhaps it will be different for the children.

But it can be a real waste of paper to dump and entire database if a field or variable of a particular record has no data or if the data field is of less than maximum length.

The following briefly summarizes programming techniques for printing data in an economical fashion. The examples use Archive procedures, but it is no trick to apply the methods to printing out data which has been stored in dimensioned arrays in SuperBASIC.

In the example we wish to print data in a file of records with three fields: A$, B$ and C$. For this illustration assume that the printed text is to be a maximum of sixty (60) characters per line and that the fields (variables) A$ and B$ are each set to have a maximum of sixty characters, but that C$ can hold up to eighty (80) characters.

First, it is necessary to write the printout procedure called print_file.

proc print_file
let z=0:rem initiates line counter variable, setting it to zero
while not eof()
lprint : let z=z+1
print_record
if z>59: lprint chr(0)+chr(26): let z=0: endif:rem performs a form feed when 59
lines have been printed and resets the line counter
endwhile
lprint chr(0)+chr(26): spoolon screen :rem sends lprint command to the screen
endproc

The counter (z) keeps track of the number of lines that are printed. Each time a record is printed the counter is tested for its value. If it is greater than 59, it performs a form feed [lprint chr(0)+chr(26)] so that there is a margin at the bottom of the page. The value can of course be changed for specific paper sizes.

Within the while loop, which terminates when there are no more records to print, there are two instructions.

lprint : let z=z+1 puts a blank line to separate the printed output of each record. Since the paper advances a line, the counter is incremented by one.

print_record is the procedure that actually causes the data to be printed, but if there is no data in the field, the paper does not advance and the line counter is not incremented.

proc print_record
if A$<>"": lprint A$: let z=z+1: endif
if B$<>"": lprint B$: let z=z+1: endif
if C$<>""
if len C$<61: lprint C$: let z=z+1
else : lprint C$ (to 60): lprint C$ (61 to ): let z=z+2: endif
endif
endproc

The above is a record printout format in its simplest form. Additional blank lines, common text, tabs, independent variables, and numerals could be inserted as desired – even printer codes to make part of a record in condensed type. Note that because of the test for a non-empty field, only lines with data will be printed.

Finally, in order to avoid using a printer, especially when testing the code for accuracy, write one more procedure.

proc output_mode
print at 0,0;"Output to Printer or Screen (p/s) ";
input z$
if instr("pP",z$): spooloff : endif
if instr("sS",z$): spoolon screen : endif
if not instr("sSpP",z$):output_mode: endif
cls :print_file
endproc

Although there are more elegant ways to write this procedure, it is so written to simplify understanding.

Press p or P and the spooloff command sends all lprint instructions to the printer. The instr() function tests for either upper or lower case. Similarly, spoolon screen sends all lprint instructions to the screen.

The final instr() forces the choice of either p or s; otherwise the procedure is called again.

Only then does the screen clear and is data printed.

For those considering adapting the procedures for SuperBASIC, spooloff and spoolon screen may be considered the equivalent of opening a channel to either ser or scr respectively.