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