ARCHIVE SERIES
PART 21 : BYCOL – A PROGRAM FOR DISPLAYING AND PRINTING DATABASES BY COLUMNS
One QL user I know uses a PC program to print out address labels because there is no QL software that will print out addresses on tractor feed labels with multiple columns. For someone who programs in ARCHIVE it is not difficult to print address labels in one, two, or more columns. I thought it might be interesting to generalize a little bit and create a program that makes it easy to print any database information in columns. Rather than create a long program with a friendly user interface using the procedures I covered in earlier parts to this series (group1,group2,group3), I made a short program that requires the user to do a little programming for each application. The complete program and instructions are included here. Also included are details explaining how the program works and a few examples using the gazet database.
Most likely you would display or print information in columns so that a large amount of information is arranged compactly but can be scanned easily. Usually the information would have an order to it. If the information were an address database then it would be arranged alphabetically by name. If we were to list addresses in columns then we could do it in 2 different ways. The addresses could be arranged so that we would scan from left to right across the columns and then down to the next row and left to right again. The second way would be to scan down the first column for a ‘page’ then up to the top of the second column and down to the bottom and then to the top of the third column and so forth. The first method (across a row) is the easiest to program since it needs no concept of page and we can just sequence through the data printing a line at a time. The second method (down a column) requires us to define a page and then construct the first column and then the second and then the third and so forth. Since the printer can’t print a column at a time we have to store an entire page then print the page a line (row) at a time.
Bycol (the program to print columns described below) is designed to organize the data in a down the column arrangement. To print to the screen you would use a page size of perhaps 24 lines. To print to the printer you would use something less than 66 like maybe 50 lines. Then each screen or page would be filled and you would read down each column to sequentially scan the data. If instead you want the data sequenced across a row then simply set the page size to 1 and the data will still be in columns but will sequence across each row (line).
To construct a page of information the program has to use an array where each element is a line of the page. It goes through the information constructing first the column and either truncating or padding out with spaces so the first column is filled to the set width of column and gap between columns. Next it goes back to the first element of the array and starts doing the same with the second column and so forth. But ARCHIVE doesn’t support explicit arrays so how is this done? I simply created a temporary database in RAM with one text field and add enough records to equal the number of lines on a page. Then the first record will hold line 1 of the page and so forth. After all the columns are put in the lines of the page the complete page is printed out (the records of this temporary database) and emptied so the next page can be constructed.
To prepare for entering the program first copy GAZET_DBF to RAM1_ and then start up ARCHIVE and get into the editor. Below you will find a listing of each procedure followed by a little explanation of what it does. After the complete program has been entered you can save it and then there are some other examples to show other things that can be done with it.
Proc bycol
spooloff : let ff_$="": let hd_$="": let pg_=1
mode 0: print " BYCOL - Print Database in Columns to Screen, Printer, or File"
print : print " Define columns and database in procedure - def_columns"
print " Define display lines in procedure - def_display"
def_columns: let gap_$=rept(" ",gap_)
print : print " Database : ";sf_$
print " Columns : ";col_;" Column Width : ";wid_;" Gap : ";gap_
print " Total width : ";col_*wid_+(col_-1)*gap_
print " Lines per display : ";lin_;" Displays per column : ";percol_
print " Lines per page : ";lin_*percol_;
print " Displays per page : ";percol_*col_
error closer;"s"
look sf_$ logical "s"
let tpg_=int(count()/(percol_*col_)+0.9999)
print " Total Pages to display complete database : ";tpg_
print : input " Print to screen (y/n) : ";y$: let y$=lower(y$)
if y$="y": spoolon screen : let out_$="Screen"
else : input " Print to printer (y/n) : ";y$: let y$=lower(y$)
if y$="y": let out_$="Printer"
input " Heading (if any) : ";hd_$
input " Use Form Feed after each page (y/n) : ";d$: let d$=lower(d$)
if d$="y": let ff_$=chr(0)+chr(12): endif
else : input " Print to file (y/n) : ";y$: let y$=lower(y$)
if y$="y"
input " Enter output device (ram1_,flp1_,etc) : ";d$
input " Enter output file name (will be overwritten) : ";out_$
if not instr(out_$,"_"): let out_$=out_$+"_lis": endif
let out_$=d$+out_$
spoolon out_$ export
else : mode 1: stop
endif : endif : endif
print : print "{printing to ";out_$;"}"
tmp_setup
let atlin_=1: let atcol_=1
use "s": first : while not eof("s")
def_display
use "s"
next
endwhile
use "t"
search val_$<>""
if found():col_out: endif
closer;"s":closer;"t"
if out_$<>"Screen"
print : input "All done. Press <ENTER> : ";d$: mode 1
endif
endproc
Bycol is the main procedure of the program. Note that many of the variable like ff_$ have an underscore (_) in their name. I did this to prevent conflicts between the program variable names and the field names of a database you might want to use the program on. I figured it was unlikely that the field names of your database will use underscore. If a program variable and your database variable do happen to have the same name then your database variable will take precedent and may interfere with the operation of the program. The spooloff/spoolon command is used to control where the lprint will send its characters. Because the program may be restarted after a crash I use spooloff above to make sure printing will go to printer unless the program redirects it. A heading along with information about the defined column display is printed. The procedure def_columns is activated. It defines the columns and records per column and the name of the database to be used. The closer procedure is used to make sure the database is closed before it is looked at. The error statement suppresses an error if it happens to not be accessed. The error statement only works on a user defined procedure so I can’t say ‘ error close “s”‘ but instead must have the close within the procedure. The user is given options for where the output is to go. The database to be used (source) is looked at and identified with the logical name “s”. If the output goes to the printer then form feeds can be used to make pages with a heading. If output goes to a file then the file is created with a spoolon statement. Any existing file of that name is automatically deleted. An extension of _lis is put on the file name if there is none. A temporary file that will hold a page of information is created in procedure tmp_setup and is given logical name “t”. So 2 databases and possibly one text file (if output goes to a file) are accessed. Next a while loop is used to sequentially (first to last) access the records of the source database. The def_display procedure has the information about the printout and is accessed for each record. A column at a time is constructed until the whole page of columns are filled and then they are printed out. This can take some time. After the last record is reached a check is made to see if any data is left unprinted. If so it is printed then the program ends.
proc closer;log_$
close log_$
endproc
Closer is a simple procedure to close a file. The passed variable log_$ is the logical name of the file to be close. When this procedure is called in bycol it has an error statement in front of it because is will be used even when the file to be closed may not be presently accessed. This would normally cause an error but the error statement suppresses the error.
proc col_out
if hd_$<>""
lprint hd_$: lprint tab 60;date(0);" ";pg_;" of ";tpg_: lprint
let pg_=pg_+1
endif
first : while not eof()
lprint val_$
let val_$="": update
next : endwhile
if ff_$<>"": lprint ff_$;: endif
let atcol_=1
endproc
Col_out is the procedure which actually prints out a page of columns. After each line of columns is printed it is cleared (set to null). The lines are stored in the temporary database. The lprint statement sends the line to the printer or screen or file depending on what the spoolon was set to (no spoolon defaults to printer).
proc col_put;tex_$
use "t"
if atlin_>percol_*lin_
let atcol_=atcol_+1: let atlin_=1: endif
if atcol_>col_
col_out
endif
position atlin_-1
if len(tex_$)>wid_: let tex_$=tex_$(1 to wid_): endif
if len(tex_$)<wid_
let tex_$=tex_$+rept(" ",wid_-len(tex_$))
endif
if atcol_<col_: let tex_$=tex_$+gap_$: endif
let val_$=val_$+tex_$: update : let atlin_=atlin_+1
use "s"
endproc
Col_put is the procedure which fills each column until all the columns of the page are filled. It then calls col_out which prints the entire page of columns. This procedure is repeatedly called by bycol. Each column is padded out or truncated to match the specified width. Each line of columns is stored in the temporary file.
proc def_columns
let col_=5: let wid_=14: let gap_=1
let lin_=2: let percol_=12
let sf_$="ram1_gazet_dbf"
endproc
def_columns is the procedure which defines the column and page and display layout of the information and also the database file to be used. The variable meanings are :
col_ - The number of columns on the page wid_ - The width in characters of the column gap_ - The gap between columns in spaces lin_ - The lines used in each record display percol_ - The number of displays in a column to make up a page. Each display may have more than one line sf_$ - The device and complete name of the source database to be displayed
This procedure needs to be modified each time you need to change the column or page or display length or file name. In this instance we have set up for a 5 column display of 2 lines long with 12 displays per column for the gazet database. This nicely fits the screen.
proc def_display
col_put;" "+country$
col_put;" "+capital$
endproc
Def_display is the procedure which controls what the display will look like for each record. The number of lines in the display need to match the lin_ variable value set in def_columns. Each text string to go on a line is constructed and passed to col_put. If the text is too long to fit the column it will be truncated there. You can use the various functions and commands of ARCHIVE to make intricate displays. This procedure needs to be modified each time you need to change a display or change to a different database.
In this instance we display the country name on line 1 and the capital (indented) on line 2.
proc tmp_setup
error closer;"t": kill "ram1_col_tmp"
create "ram1_col_tmp" logical "t"
val_$
endcreate
let i=1: while i<=percol_*lin_
let val_$="": append
let i=i+1: endwhile
endproc
Tmp_setup deletes the old temporary file if it exists and creates a new one. Each record has a single text field called “val_$” which will hold values from all columns for that particular line. Enough records are created to hold the correct number of displays to go in a column where each display consists of lin_ lines.
Example 1
This is the complete program. Now save it as “bycol”. Try running it by typing :
bycol<ENTER>
Correct any mistakes. If it works right you will be asked if print should go to screen. Answer “y”. Notice it takes a while for each screen to be constructed. There are 60 displays (records) on the screen. Remember to have gazet_dbf in RAM.
Now try changing the def_col procedure so per_col_=25. This will make the page have 50 lines (lin_*per_col_). Now trying having the output go to the printer. Use “Country and Capitals” for the heading and use form feeds. Notice it takes even longer as there are now 125 displays per page.
Example 2
Now try changing def_columns and def_display as follows. This will print more information about each country. Notice that the number fields (pop, gdp, area) are changed to text before being passed to put_col.
proc def_columns
let col_=3: let wid_=20: let gap_=3
let lin_=6: let percol_=9
let sf_$="ram1_gazet_dbf"
endproc
proc def_display
col_put;" "+country$
col_put;" "+capital$
col_put;" "+languages$
col_put;" "+currency$
col_put;gen(pop,6)+gen(gdp,6)+gen(area,6)
col_put;""
endproc
Example 3
Now try changing def_columns and def_display as show below. It arranges country and capital on a single line in columns.
proc def_columns
let col_=2: let wid_=39: let gap_=1
let lin_=1: let percol_=24
let sf_$="ram1_gazet_dbf"
endproc
proc def_display
let i$=country$: let i=len(i$)
if i>20: let i$=i$(1 to 20)
else : let i$=i$+rept(" ",20-i): endif
col_put;i$+" "+capital$
endproc
Example 4
Now suppose you had an address database. You might have different field names than used in the example below. If so then modify as needed. This example prints out our sought after address labels in columns. This is set up for the screen but can be easily altered for printer. To print to paper probably set percol_=9 with a heading and form feed. To print to tractor fee labels set percol=1 and no heading or form feed. Remember to put your address database in RAM.
proc def_columns
let col_=3: let wid_=25: let gap_=1
let lin_=6: let percol_=12
let sf_$="ram1_address_dbf"
endproc
proc def_display
col_put;" "+fname$+" "+name$
col_put;" "+street1$
if street2$<>"":col_put;" "+street2$: endif
col_put;" "+city$+", "+state$+" "
col_put;" "+zip$
if street2$="":col_put;"": endif
col_put;""
endproc
Example 5
To print a handy phone book type list from an address database use the following settings.
proc def_columns
let col_=2: let wid_=39: let gap_=1
let lin_=1: let percol_=24
let sf_$="ram1_address_dbf"
endproc
proc def_display
let i$=" "+name$+" "+fname$: let i=len(i$)
if i>24: let i$=i$(1 to 24)
else : let i$=i$+rept(" ",i-24): endif
col_put;" "+i$+" "+phone$
endproc
Play around with other databases too. There are many useful things you can do. Next time more fun with ARCHIVE. Any suggestions are welcomed. Until then, Happy Archiving!
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.