CHAPTER 5: Printing Tables


The next script illustrates the principle of producing compact tables. When laying out tabular data by hand, the typist's headache has always been column widths. Tables often require columns as narrow as possible, which means that the width of each column entry must be checked to find which is the largest, and the column spacings set accordingly. Although tedious, this is not difficult in principle when all the data may be examined beforehand. Many report writers, however, do not allow multiple passes through data for this kind of checking, so columns must by default be set wide enough to take the largest possible value for the attribute, rather than the largest actual value.

Empress Report Writer solves this problem with the MAXWIDTH function, which finds the widest value retrieved for the given attribute. Hence, the width necessary for each column in a table may be found before the table is started, and suitable spacing between columns then determined. This also allows checking whether it will be possible to actually fit the entire table across the page.

The attribute value may be qualified with a WHERE clause if desired.

The following script prints out the job types and pay rates from the rates table in compact form:

LET DATABASE = "repairs";
SELECT FROM rates;

LET jobwidth = MAXWIDTH job_type;
FOR EACH RECORD
	PRINT job_type WIDTH JOBWIDTH, " ",
	RATE PIC "G$F9V.99", NEWLINE;
END;

It produces:

Doors               $25.00
Windshields         $20.00
Lights              $15.00
Fenders             $20.00
Axles               $20.00
Lubrication         $15.00
Ignition/sparks     $20.00
Wheels              $20.00
Tires               $15.00
Transmission        $25.00
Body work           $25.00
Brakes              $20.00
Universal           $25.00
Exhaust             $20.00
Battery             $15.00
Shocks              $15.00
Pistons             $15.00
Carburetor          $20.00
Radiator            $20.00
Distributor         $20.00

The attribute job_type is specified as CHAR (20,1), so the above table saves five spaces (the widest entry being 15 characters). By contrast, if the job types are just printed without specifying a column width, the report script is as follows:

LET DATABASE = "repairs";
SELECT FROM rates;

FOR EACH RECORD
	PRINT job_type, "  ",
	RATE PIC "G$F9V.99", NEWLINE;
END;

It produces:

Doors  $25.00
Windshields  $20.00
Lights  $15.00
Fenders  $20.00
Axles  $20.00
Lubrication  $15.00
Ignition/sparks  $20.00
Wheels  $20.00
Tires  $15.00
Transmission  $25.00
Body work  $25.00
Brakes  $20.00
Universal  $25.00
Exhaust  $20.00
Battery  $15.00
Shocks  $15.00
Pistons  $15.00
Carburetor  $20.00
Radiator  $20.00
Distributor  $20.00

which is unsatisfactory as a tabular layout.