Updated:
After my initial post and responses, I managed to have another crack and have written out my aims and results a bit clearer:
Aim:
I'm attempting to count the number of hits in a search string of a log file to figure out how many occurrences of a message are generated in the following ways:
- Total per day.
- Total per hour.
- Highest per min, per hour.
- Highest per sec, per hour.
My working code:
#!/usr/bin/perl
#use strict;
use warnings;
use Data::Dumper;
my @a = (
[ qw /2012-02-21_09:43:43/ ],
[ qw /2012-02-21_09:43:43/ ],
[ qw /2012-02-21_09:43:44/ ],
[ qw /2012-02-21_09:43:44/ ],
[ qw /2012-02-21_09:43:44/ ],
[ qw /2012-02-21_09:43:45/ ],
[ qw /2012-02-21_09:43:45/ ],
[ qw /2012-02-21_09:43:45/ ],
[ qw /2012-02-21_09:43:45/ ],
[ qw /2012-02-21_09:44:47/ ],
[ qw /2012-02-21_09:44:47/ ],
[ qw /2012-02-22_09:44:49/ ],
[ qw /2012-02-21_10:44:49/ ]
);
my ( %count, $count ) = ();
foreach (@a) {
my $line = @$_[0] ;
$line =~ /(\S+)_(\d+):(\d+):(\d+)/ ;
my $day = $1;
my $hour= $2;
my $min = $3;
my $sec = $4;
$count {$day}->{$hour}->{$min}->{$sec}{'sec'} += 1 ;
$count {$day}->{$hour}->{$min}{'min'} += 1 ;
$count {$day}->{$hour}{'hour'} += 1 ;
$count {$day}{'day'} += 1 ;
}
#print Dumper (%count) . "\n";
foreach my $k1 ( sort keys %count ) {
print "$k1\t$count{$k1}{'day'}\n" ;
foreach my $k2 ( sort keys %{$count{$k1}} ) {
if ($k2 =~ /day/) {
next;
}
print " $k2:00\t\t$count{$k1}{$k2}->{'hour'}\n";
foreach my $k3 ( sort keys %{$count{$k1}{$k2}} ) {
if ($k3 =~ /hour/) {
next;
}
print " $k2:$k3\t\t$count{$k1}{$k2}{$k3}->{'min'}\n";
foreach my $k4 ( sort keys %{$count{$k1}{$k2}{$k3}} ) {
if ($k4 =~ /min/) {
next;
}
print " $k2:$k3:$k4\t$count{$k1}{$k2}{$k3}{$k4}->{'sec'}\n";
}
print "\n";
}
print "\n";
}
}
exit;
Results
I've had to turn off strict (of which I am ashamed), due to my poor hash dereference methods.
2012-02-21 12
09:00 11
09:43 9
09:43:43 2
09:43:44 3
09:43:45 4
09:44 2
09:44:47 2
10:00 1
10:44 1
10:44:49 1
Attempting to output:
2012-02-21 12
09:00 11
09:43 9
09:43:45 4
10:00 1
10:44 1
10:44:49 1
Questions:
- Is there a better way of writing the code, and turning on strict?
- How would I go about listing the highest occurrence of a hash value within a hash, in an attempt to list the highest number count only?
Thanks for all the previous posts, I couldn't have gotten this far without them.
Cheers,
Andy