vatheck has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl #pragmas use strict; use warnings; #modules use File::Find::Rule; #declaring global variables use vars qw/$reportfile $lastday @datadir @sites @dates %devicecount @ +workingdata/; #set up the datadir array @datadir = qw[/dir1 /dir2 /dir3 /dir4 /dir5 /dir6 /dir7]; #set up the last day variable for file::find::rule my $lastday = time()-86400; #get a list of sites { my %uniquehash; my @devicefilelist = File::Find::Rule->file()->name('*SomeText*')- +>mtime("<$lastday")->in(@datadir); foreach (@devicefilelist) { if (open FILE, "$_") { while (<FILE>) { my $firstsplit = (split /,/, $_)[2]; my $site = (split /M/, $firstsplit)[0]; $uniquehash{$site} = 1; } close FILE; } } @sites = sort keys %uniquehash; chomp @sites; } #get a list of dates { my %uniquehash; my @datefilelist = File::Find::Rule->file()->name('*SomeText*')->i +n(@datadir); foreach (@datefilelist) { my $date = (split /_/, $_)[1]; $uniquehash{$date} =1; } @dates = sort keys %uniquehash; } #get a device count for each site { my %uniquehash; my @devicecountlist = File::Find::Rule->file()->name('*Texthere*') +->mtime("<$lastday")->in(@datadir); foreach my $file (@devicecountlist) { if (open FILE, $file) { while (<FILE>) { if (!/SomethingIDontWant/ && !/SecondThingIdontWant/ & +& !/ThirdThingIdontWant/) { my $device = (split /,/, $_)[2]; $uniquehash{$device} = 1; } } close FILE; } } foreach my $site (@sites) { foreach (keys %uniquehash) { if (/${site}b/) { $devicecount{$site} ++; } } } } #the main statistics routine { my ($site, $devicecount, $value, $counter, @workingdata, @workingd +ay); my @allfilenames = File::Find::Rule->file()->name( qr/Site-Calls/ +)->in(@datadir); open REPORT, ">>", &gen_reportfile_name(); foreach my $date (@dates) { @workingday = grep /$date/, @allfilenames; @workingdata = (); foreach my $file (@workingday) { if (open CURRENT, $file) { while (<CURRENT>) { push @workingdata, $_; } close CURRENT; } } foreach (0..23) { my $hour = sprintf "%02d", $_; while (($site, $devicecount) = each %devicecount) { $value = 0; $counter =0; foreach (@workingdata) { if (/${date}-${hour}/ && /$site/) { my @Fld = split /,/; $value += $Fld[11]; $counter++; } } print REPORT "$site\t$bscount\t${date}-${hour}\t"; if ($counter == 0) {print REPORT "0\n"} ÷byzero($counter, $value); } } } } #divide by 0 subroutine sub dividebyzero { if (($_[0] > 0) && ($_[1] > 0)) { print REPORT $_[1]/$_[0] . "\n"; } elsif (($_[0] > 0) && ($_[1] == 0)) { print REPORT "0\n"; } } #generate the reportfile name sub gen_reportfile_name { my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(ti +me); $reportfile = sprintf "/dirhere/%4d%02d%02dtalkpath", $year+1900,$ +mon+1,$mday; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: speed issues
by samtregar (Abbot) on May 16, 2008 at 03:42 UTC | |
by vatheck (Novice) on May 16, 2008 at 03:52 UTC | |
|
Re: speed issues
by 5mi11er (Deacon) on May 16, 2008 at 04:01 UTC | |
|
Re: speed issues
by CountZero (Bishop) on May 16, 2008 at 05:16 UTC | |
|
Re: speed issues
by apl (Monsignor) on May 16, 2008 at 09:48 UTC | |
|
Re: speed issues
by toolic (Bishop) on May 16, 2008 at 13:08 UTC |