Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Here's a version that (shouldn't) change the functionality, but cleans up some the code with "more perlish" code .. especially the regex usage (use $x =~ /foo/ instead of assigning to $_) and some of the if-else logic (most notably using grep). Diff against your original to see all the individual differences .. But hopefully this will shorten and clarify the code so it's easier for you to go from there...
#! /usr/local/bin/perl use warnings; use strict; ########################## Execution Setup ########################### +########### use Cwd; # Local Modules ###################################################################### +########### # File: $PWD/gcenrollrpt + # # Purpose: Retrieve logs creates a daily report of various gc statisti +cs # # Usage: gcenrollrpt date YYYY-MM-DD + # # Notes: + # ###################################################################### +########### system("date"); # Command Line Syntax Checking if ( (! $ARGV[0] ) ) { print "\nUSAGE: $0 <date>\n"; exit 1; } # set primary vars my $today = `date +%Y-%m-%d`; chomp($today); my $date = $ARGV[0]; chomp($ARGV[0]); my $host = `uname -n`; chomp($host); my @banks = qw[ 321 920 144 ]; # Read in statements to be searched for in log from ini file my $iniFile = "$ENV{'PWD'}/gcenrollrpt.ini"; open(INIFILE, $iniFile) or die "Can't open $iniFile $!\n"; my @entries = <INIFILE>; # Determine which log to read my ($logfile, @logfile); my $rmfile=0; my $archive = "/log_archive/GC/$host/GC/logs/GC.log.$date.gz"; if ( -e "/GC/logs/GC.log.$date" ) { $logfile = "/GC/logs/GC.log.$date"; } elsif ( -e $archive ) { system("gzcat $archive > /GC/logs/GC.log.$date" ); $logfile = "/GC/logs/GC.log.$date"; $rmfile = 1; } else { $logfile = "/GC/logs/GC.log"; } # Parse the log my (%counts, @rptType); open( LOGFILE, $logfile ) or die "Can't open file $logfile $!\n"; LOG:while ( my $logEntry = <LOGFILE> ) { next LOG unless $logEntry =~ /^ $date/; foreach my $bank (@banks) { # check each entry from the ini file against the log ENTRY:foreach my $entryTemplate ( @entries ) { # split the entry type from the actual verbage to be search +ed for chomp($entryTemplate); next unless length($entryTemplate); my ( $rptType, $entry ) = $entryTemplate =~ /(.+)~(.+)/; # make the search bank specific $entry =~ s/\$bank/$bank/g; next unless length($entry) && length($rptType); # set up counters for each type and store unique types for + each bank # serves as an index when outputting results # push @rptType, $rptType unless @rptType; # UPDATE -- r +emove this line -- it's unnecessary push @rptType, $rptType unless grep /$rptType/, @rptType; # set up the key $typeHour for the %counts var that stores + all the totals # must be done first to set up counts for entries which th +ere are no matches my $hour = $logEntry =~ /\d{4}-\d{2}-\d{2}\s(\d{2}):/; my $typeHour = "$bank-$rptType-$hour"; # Process Matching entries $counts{$typeHour} ||= 0; $counts{$typeHour}++ if $logEntry =~ $entry; } } } close (LOGFILE); # Remove the log file if unzipped from the archive system("/usr/bin/rm", "-f", $logfile) if ( $rmfile == 1 ); # Create reports by bank foreach my $bank (@banks) { my $tmpFile1 = "/tmp/gcenrollrpt$bank.1.$$"; open ( TMPFILE1, ">", $tmpFile1 ) or die "Can't open $tmpFile1 $!\n +"; foreach my $type (@rptType) { foreach my $i ( 0..23 ) { foreach my $typeHour ( sort keys %counts ) { my ($bnk,$rptType,$hr) = $typeHour =~ /(.+)-(.+)-(.+)/; my $hour = sprintf "%02d", $i; next unless "$bank-$type-$hour:" =~ /$bnk-$rptType-$hr:/; print TMPFILE1 ( $i ? "$hour: $counts{$typeHour}\n" : "\n\ +n$type\n" ); } } } close(TMPFILE1); # Mail the report out my $cmdLine = "cat $tmpFile1 | mailx -s \"OAC report for bank $bank + $date $host\" xxx\@xyz.com"; system("$cmdLine"); system("/usr/bin/rm -f $tmpFile1"); } system("date"); 0;

In reply to Re: Pls help optomize - ksh version 60% faster! by davidrw
in thread Pls help optomize - ksh version 60% faster! by coreolyn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-19 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found