next up previous contents
Next: Report Headings Up: Reports Previous: Format Statements

Field Lines

The field lines of a format statement control what is displayed and how. The simplest field line contains only static text. You can use static or unchanging text as labels for variable information, dollar signs in front of amounts, a separator character such as a comma between first and last name, or whatever else is needed. However, you'll rarely use just static text in your format statement. Most likely, you'll use a mix of static text and field holders.

You saw a field holder in action in the last section in which I demonstrated sending the report to STDOUT. I'll repeat the format statement here so you can look at it in more detail:

format =

    The total amount is $@###.##

                         $total

The character sequence The total amount is $ is static text. It will not change no matter how many times the report is printed. The character sequence @###.##, however, is a field holder. It reserves seven spaces in the line for a number to be inserted. The third line is the value line; it tells Perl which variable to use with the field holder.

The following format characters can be uses in field lines.

Let's start using some of these formatting characters by formatting a report to display information about the FORMAT.DAT file we used earlier.

We will display the information in nice, neat columns. as follows:

The Perl code (report3.pl) is as follows:

format =

  Album=@<<<<<<<<<<<<<  Artist=@>>>>>>>>>>>>  Price=$@##.##

        $album,                $artist,              $price

.



open(FILE, "<format.dat");

@lines = <FILE>;

close(FILE);



foreach (@lines) {

    chop;

    ($album, $artist, $price) = (split(/!/));

    $album  = "" if !defined($album);

    $artist = "" if !defined($artist);

    $price  = 0 if !defined($price);

    write();

}

This program displays the following:

  Album=A Love Supreme  Artist=                      Price=$  0.00
  Album=A Kind of Blue  Artist=   Miles Davis        Price=$9.99
  Album=Koln Concert    Artist=    Keith Jarrett     Price=$  15.99
  Album=Birds of Fire   Artist= Mahvishnu Orchestra  Price=$ 10.99

You can see that the columns are now neatly aligned. This was done with the format statement and the write() function. The format statement used in this example used three field holders. The first field holder,

@<<<<<<<<<<<<<,

created a left-justified spot for a 14-character-wide field filled by the value in $album.

The second field holder,

@>>>>>>>>>>>>,

created a right-justified spot for a 12-character-wide field filled by the value in $artist.

The last field holder,

@##.##,

created a six-character-wide field filled by the numeric value in $price.

You might think it's wasteful to have the field labels repeated on each line, and I would agree with that. Instead of placing field labels on the line, you can put them in the report heading. The next section discusses how to do this.


next up previous contents
Next: Report Headings Up: Reports Previous: Format Statements
dave@cs.cf.ac.uk