From: gheeong@rhea.trl.OZ.AU (Ghee Ong)
Subject: repost - reset page number but form feed missing
Keywords: perl,report,page,form-feed
Organization: Telecom Research Labs, Melbourne, Australia
Date: Tue, 24 Nov 1992 02:56:26 GMT


Hi there,

(I am re-posting this because there was a problem with our news feed.)
I am a novice on perl. As an exercise, I wrote this report program.
One problem and one question below I need help on, viz

(1) the report is of the form details for a section/branch followed by
  a summary. For a new section/branch, the page number is reset to 1.
  I found that there is no form-feed char when I forced to reset page number
  and print on new page.

                   header1                       page 1
   section XXX
   branch  YYY

          user      machine     service      amount
          ----      -------     -------      ------




****form-feed here
                   header1                       page n
   section ........
   branch  .......
   
                   SUMMARY BY SERVICES
                   ===================

          service       machine         amount
          -------       -------         ------


****no form-feed found here
                   header1                       page 1
   section XXZ
   branch  YYY

          user      machine     service      amount
          ----      -------     -------      ------

(2) question - is there a format character that suppresses printing of repeating
 fields eg

       instead of   xxxxx    yyyyy
                    xxxxx    zzzzz

       I want report to look like

                    xxxxx    yyyyy
                             zzzzz

Any other comments on the programs welcomed.  Thanks in anticipation.
--
Ghee Ong(g.ong@trl.oz.au)
-----------------------------report program----------------------------------
#!/usr/local/bin/perl

chop($Date = `date +%d/%m/%y`);

sub dosummary {
              $^ = 'STDOUT_TOP2' ;
              $~ = 'STDOUT_F2' ;
              $- = 0 ;
              $sk2 = '' ;
              $sum_srvaa = 0 ;
              foreach $sk (sort keys(%sum_srvat)) {
                ($sum_srvt,$sum_mac) = split(/,/,$sk);
                ($sum_srvt2,$sum_mac2) = split(/,/,$sk2);
                if ( $sum_srvt ne $sum_srvt2  && $sk2 ne '') {
                   $~ = 'STDOUT_F3';
                   write ;
                   $~ = 'STDOUT_F2' ;
                   $sum_srvaa = 0 ;
                }
                $sum_srva = $sum_srvat{$sk} ;
                write;
                $sum_srvaa += $sum_srva ;
                $sk2 = $sk ;
              }
              $~ = 'STDOUT_F3';
              write ;
              $~ = 'STDOUT_F2' ;
              %sum_srvat = '' ;
              $% = 0 ;
              $^ = 'STDOUT_TOP' ;
              $~ = 'STDOUT' ;
              $- = 0 ;
}

$ecnt = 0 ;
while (<STDIN>) {
  chop ;
  ++$rcnt ;

  ($d1,$year,$mth,$br,$sect,$unm,$ags,$lognm,$mac,$srvt,$srva,$d2)
         = split(/:/,$_);
# printf("%s %s %s %s %s %s %s\n",$lognm,$ags,$mac,$mth,$year,$srvt,$srva);

# check for errors
  $err = '' ;
  $ags !~ /[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/
           && ($err = "\tfield ags -- not numeric\n") ;
  $year !~ /[0-9][0-9]/  && ($err .= "\tfield year -- not numeric\n") ;
  $srva eq '' &&  ($err .= "\tfield service amount - invalid\n") ;

  if ( $err ne '' ) {
     ++$ecnt ;
     printf(STDERR "%s\n%s",$_,$err);
 #print "service amt = $srva\n";
 #printf("%s %s %s %s %s %s %s\n",$lognm,$ags,$mac,$mth,$year,$srvt,$srva);
  }
  else {
     $mth =~ tr/'a-z'/'A-Z'/ ;
     $k1 = $lognm.','.$mac.','.$srvt ;
     if ( $k1 eq $k2 ) {
        $srvau += $srva ; 
     }
     else {
        if (defined($first)) {
           ($lognmu,$macu,$srvtu) = split(/,/,$k2);
           write ;
           $sk = $srvtu.','.$macu ;
           $sum_srvat{$sk} += $srvau ;
           if ($sect ne $sect2)   {
              &dosummary() ;
           }
        }
        else {
           $first = 1;
        }
        $srvau = $srva ;
     }
     $k2 = $k1 ;
     $sect2 = $sect ;
     $br2 = $br ;
  }
}

if ( %sum_srvat > 0 ) {
   &dosummary();
}

print "read = $rcnt error = $ecnt\n";

exit;
##############################################################################
format STDOUT_TOP =
@<<<<<<<<                 TRL Computer Resource Usage            Page : @### 
$Date,                                                                  $%
                         Detail Level Report For @<<< @##
                                                 $mth,$year

Section: @<<<<<<<<<<<<<<<<<<<<<<<<<<
         $sect2
Branch : @<<<<<<<<<<<<<<<<<<<<<<<<<<
         $br2

        User          Machine           Service            Amount
        ----          -------           -------            ------
.

format STDOUT =
        @<<<<<<<<     @<<<<<<<<<<<<<    @<<<<<<<<<<<<<  @#####.##
        $lognmu,      $macu,            $srvtu,         $srvau
.

format STDOUT_TOP2 =
@<<<<<<<<                 TRL Computer Resource Usage            Page : @### 
$Date,                                                                  $%
                         Detail Level Report For @<<< @##
                                                 $mth,$year

Section: @<<<<<<<<<<<<<<<<<<<<<<<<<<
         $sect2
Branch : @<<<<<<<<<<<<<<<<<<<<<<<<<<
         $br2

                      SUMMARY BY SERVICES
                      ===================


             Service       Machine             Amount
             -------       -------             -------
.

format STDOUT_F2 =
             @<<<<<<<<     @<<<<<<<<<<<<<    @#####.##
             $sum_srvt,    $sum_mac,         $sum_srva
.

format STDOUT_F3 =
                                             ---------
                                             @#####.##
                                             $sum_srvaa

.