home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / perl / 7118 < prev    next >
Encoding:
Text File  |  1992-11-24  |  5.9 KB  |  210 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!munnari.oz.au!titan!rhea!gheeong
  3. From: gheeong@rhea.trl.OZ.AU (Ghee Ong)
  4. Subject: repost - reset page number but form feed missing
  5. Message-ID: <1992Nov24.025626.10511@trl.oz.au>
  6. Keywords: perl,report,page,form-feed
  7. Sender: root@trl.oz.au (System PRIVILEGED Account)
  8. Organization: Telecom Research Labs, Melbourne, Australia
  9. Date: Tue, 24 Nov 1992 02:56:26 GMT
  10. Lines: 198
  11.  
  12.  
  13. Hi there,
  14.  
  15. (I am re-posting this because there was a problem with our news feed.)
  16. I am a novice on perl. As an exercise, I wrote this report program.
  17. One problem and one question below I need help on, viz
  18.  
  19. (1) the report is of the form details for a section/branch followed by
  20.   a summary. For a new section/branch, the page number is reset to 1.
  21.   I found that there is no form-feed char when I forced to reset page number
  22.   and print on new page.
  23.  
  24.                    header1                       page 1
  25.    section XXX
  26.    branch  YYY
  27.  
  28.           user      machine     service      amount
  29.           ----      -------     -------      ------
  30.  
  31.  
  32.  
  33.  
  34. ****form-feed here
  35.                    header1                       page n
  36.    section ........
  37.    branch  .......
  38.    
  39.                    SUMMARY BY SERVICES
  40.                    ===================
  41.  
  42.           service       machine         amount
  43.           -------       -------         ------
  44.  
  45.  
  46. ****no form-feed found here
  47.                    header1                       page 1
  48.    section XXZ
  49.    branch  YYY
  50.  
  51.           user      machine     service      amount
  52.           ----      -------     -------      ------
  53.  
  54. (2) question - is there a format character that suppresses printing of repeating
  55.  fields eg
  56.  
  57.        instead of   xxxxx    yyyyy
  58.                     xxxxx    zzzzz
  59.  
  60.        I want report to look like
  61.  
  62.                     xxxxx    yyyyy
  63.                              zzzzz
  64.  
  65. Any other comments on the programs welcomed.  Thanks in anticipation.
  66. --
  67. Ghee Ong(g.ong@trl.oz.au)
  68. -----------------------------report program----------------------------------
  69. #!/usr/local/bin/perl
  70.  
  71. chop($Date = `date +%d/%m/%y`);
  72.  
  73. sub dosummary {
  74.               $^ = 'STDOUT_TOP2' ;
  75.               $~ = 'STDOUT_F2' ;
  76.               $- = 0 ;
  77.               $sk2 = '' ;
  78.               $sum_srvaa = 0 ;
  79.               foreach $sk (sort keys(%sum_srvat)) {
  80.                 ($sum_srvt,$sum_mac) = split(/,/,$sk);
  81.                 ($sum_srvt2,$sum_mac2) = split(/,/,$sk2);
  82.                 if ( $sum_srvt ne $sum_srvt2  && $sk2 ne '') {
  83.                    $~ = 'STDOUT_F3';
  84.                    write ;
  85.                    $~ = 'STDOUT_F2' ;
  86.                    $sum_srvaa = 0 ;
  87.                 }
  88.                 $sum_srva = $sum_srvat{$sk} ;
  89.                 write;
  90.                 $sum_srvaa += $sum_srva ;
  91.                 $sk2 = $sk ;
  92.               }
  93.               $~ = 'STDOUT_F3';
  94.               write ;
  95.               $~ = 'STDOUT_F2' ;
  96.               %sum_srvat = '' ;
  97.               $% = 0 ;
  98.               $^ = 'STDOUT_TOP' ;
  99.               $~ = 'STDOUT' ;
  100.               $- = 0 ;
  101. }
  102.  
  103. $ecnt = 0 ;
  104. while (<STDIN>) {
  105.   chop ;
  106.   ++$rcnt ;
  107.  
  108.   ($d1,$year,$mth,$br,$sect,$unm,$ags,$lognm,$mac,$srvt,$srva,$d2)
  109.          = split(/:/,$_);
  110. # printf("%s %s %s %s %s %s %s\n",$lognm,$ags,$mac,$mth,$year,$srvt,$srva);
  111.  
  112. # check for errors
  113.   $err = '' ;
  114.   $ags !~ /[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/
  115.            && ($err = "\tfield ags -- not numeric\n") ;
  116.   $year !~ /[0-9][0-9]/  && ($err .= "\tfield year -- not numeric\n") ;
  117.   $srva eq '' &&  ($err .= "\tfield service amount - invalid\n") ;
  118.  
  119.   if ( $err ne '' ) {
  120.      ++$ecnt ;
  121.      printf(STDERR "%s\n%s",$_,$err);
  122.  #print "service amt = $srva\n";
  123.  #printf("%s %s %s %s %s %s %s\n",$lognm,$ags,$mac,$mth,$year,$srvt,$srva);
  124.   }
  125.   else {
  126.      $mth =~ tr/'a-z'/'A-Z'/ ;
  127.      $k1 = $lognm.','.$mac.','.$srvt ;
  128.      if ( $k1 eq $k2 ) {
  129.         $srvau += $srva ; 
  130.      }
  131.      else {
  132.         if (defined($first)) {
  133.            ($lognmu,$macu,$srvtu) = split(/,/,$k2);
  134.            write ;
  135.            $sk = $srvtu.','.$macu ;
  136.            $sum_srvat{$sk} += $srvau ;
  137.            if ($sect ne $sect2)   {
  138.               &dosummary() ;
  139.            }
  140.         }
  141.         else {
  142.            $first = 1;
  143.         }
  144.         $srvau = $srva ;
  145.      }
  146.      $k2 = $k1 ;
  147.      $sect2 = $sect ;
  148.      $br2 = $br ;
  149.   }
  150. }
  151.  
  152. if ( %sum_srvat > 0 ) {
  153.    &dosummary();
  154. }
  155.  
  156. print "read = $rcnt error = $ecnt\n";
  157.  
  158. exit;
  159. ##############################################################################
  160. format STDOUT_TOP =
  161. @<<<<<<<<                 TRL Computer Resource Usage            Page : @### 
  162. $Date,                                                                  $%
  163.                          Detail Level Report For @<<< @##
  164.                                                  $mth,$year
  165.  
  166. Section: @<<<<<<<<<<<<<<<<<<<<<<<<<<
  167.          $sect2
  168. Branch : @<<<<<<<<<<<<<<<<<<<<<<<<<<
  169.          $br2
  170.  
  171.         User          Machine           Service            Amount
  172.         ----          -------           -------            ------
  173. .
  174.  
  175. format STDOUT =
  176.         @<<<<<<<<     @<<<<<<<<<<<<<    @<<<<<<<<<<<<<  @#####.##
  177.         $lognmu,      $macu,            $srvtu,         $srvau
  178. .
  179.  
  180. format STDOUT_TOP2 =
  181. @<<<<<<<<                 TRL Computer Resource Usage            Page : @### 
  182. $Date,                                                                  $%
  183.                          Detail Level Report For @<<< @##
  184.                                                  $mth,$year
  185.  
  186. Section: @<<<<<<<<<<<<<<<<<<<<<<<<<<
  187.          $sect2
  188. Branch : @<<<<<<<<<<<<<<<<<<<<<<<<<<
  189.          $br2
  190.  
  191.                       SUMMARY BY SERVICES
  192.                       ===================
  193.  
  194.  
  195.              Service       Machine             Amount
  196.              -------       -------             -------
  197. .
  198.  
  199. format STDOUT_F2 =
  200.              @<<<<<<<<     @<<<<<<<<<<<<<    @#####.##
  201.              $sum_srvt,    $sum_mac,         $sum_srva
  202. .
  203.  
  204. format STDOUT_F3 =
  205.                                              ---------
  206.                                              @#####.##
  207.                                              $sum_srvaa
  208.  
  209. .
  210.