9.10 Advanced Printing Techniques

Until now, we have not been doing fancy printing. The Report Writer PRINT statement has many features and options. In this section, you have the opportunity to explore this statement in depth.

Justification

The PRINT statement has the ability to justify values to one of three points: left, right, or center. To justify a value, you must specify a field width (in columns). Unused columns in the field are printed as blanks. Left justifying a value prints the value starting at the left end of the field. Right justifying a value prints the value ending at the right end of the field. Center justifying a value prints the value with an even number of blanks on either side of the value. The table below gives some examples of justification:

Left Center Right

123 123 123

word word word

a phrase a phrase a phrase

The keywords that specify justification are: LEFT_JUST, RIGHT_JUST, and CENTERED. These keywords may appear only after a field width, as in:

PRINT ssn:20 LEFT_JUST, NL;

PRINT part:20 RIGHT_JUST, NL;

PRINT title:40 CENTERED, NL;

If justification (LEFT_JUST, RIGHT_JUST, or CENTERED) is not specified, default justifications are used. For numeric types (real, money and integer), right justification is used. All other types use left justification. For example, if amount is a money value and today is a date value, the following PRINT statements are equivalent:

PRINT date:10, amount:8;

and

PRINT date:10 RIGHT_JUST,

amount:8 LEFT_JUST;

There is a shorthand notation allowed for reversing the default justification for one item. If the field width argument is negative, the opposite justification is used (i.e., right justification becomes left, left justification becomes right). When justifications are specified (LEFT_JUST, etc.), the sign of the field width is ignored.

Justification Example

All of the reports up to now have tried to get headings more or less in the center of the page. This was done manually by counting the characters in the title and picking a TAB column that would roughly center the title on the page. With justification, centering titles becomes easy. For example, we can replace the headings procedure from our previous example with the following:

PROCEDURE headings

BEGIN

title = "S u b s c r i p t i o n L i s t"

PRINT title : 60 CENTERED,

$todays_date:10, "Page ", $page, NL;

PRINT "Subscriber name", TAB (36),

"Magazine", TAB (52), "Date Started",

TAB (68), "Amount" : 10 RIGHT_JUST,

NL, NL;

END

Notice how the Amount heading is printed. When the procedure details prints the amount, it uses a statement like:

PRINT TAB (68), amount_due:10;

This statement prints the amount_due variable in a field width of 10. Because this is a number, the value is right justified in the field. Our modified headings procedure uses the same field width, TAB stop, and right justifies the heading to match the details procedure. This makes it easier to modify the report.

Data Formatting

All of the C/Base programs have the ability to input and display data values in a format specific to the conventions of a particular country. Rather than hard-coding this into the programs, a flexible method was developed that should allow any conventions to be followed. As a result, the formatting routines have formatting features that, while not convenient for input, can be very useful for output. The Report Writer allows you to access these features when printing data values.

The USING print item allows you to specify what formatting to use when printing a value. The syntax of this item looks like:

expression1 USING expression2

Expression1 is evaluated and becomes the value to be formatted. Expression2 is evaluated and becomes the format string controlling the formatting of the value expression1). Some examples of this might be:

PRINT amount USING "$*###,##0.00-"

PRINT isRight USING "T;F"

IF fulldates THEN

format = "dddd mmm dd yyyy"

ELSE

format = "dd-mmm-yy"

PRINT ordered+10 USING format;

The following table shows the output from each PRINT statement with several different values:

Variable

Name Type Value Result

amount money 901.31 $****901.31

amount money 3489.75 $**3,489.31

amount money -10.97 $*****10.97-

isRight boolean yes T

isRight boolean no F

fulldates boolean yes Monday Jan 23 1989

ordered date Jan 23, 1989

fulldates boolean no 23-Jan-89

ordered date Jan 23, 1989

Chapter 13, Formatting Data Values discusses formatting rules and features in much greater depth. Suffice to say that any valid formatting string can be used when printing a value with the USING print item. You do need to make sure that the formatting string is appropriate for the value's data type (date formats for dates, etc.)

The USING print item prints one value. In dialects of other languages (most notably, BASIC), the PRINT USING construct can print one or more values. In the Report Writer, the USING construct prints only one value. You can combine several of these in one PRINT statement, as in:

PRINT $todays_date USING "ddd mmm dd ",

$todays_time USING "h:mm AM;h:mm PM",

$todays_date USING "yyyy"

This produces output like:

Mon Jan 23 8:53 AM 1989

There are more examples of advanced printing in the following sections.