更新:
在我最初的帖子和回复之后,我又成功了,并且更清楚地写出了我的目标和结果:
目标:
我正在尝试计算日志文件搜索字符串中的命中次数,以计算通过以下方式生成消息的次数:
- 每天总计。
- 每小时总计。
- 最高每分钟、每小时。
- 最高每秒,每小时。
我的工作代码:
#!/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;
结果
由于我糟糕的散列解引用方法,我不得不关闭严格(对此我感到羞愧)。
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
正在尝试输出:
2012-02-21 12
09:00 11
09:43 9
09:43:45 4
10:00 1
10:44 1
10:44:49 1
问题:
- 有没有更好的方法来编写代码并启用严格?
- 我将如何着手列出散列值中出现次数最多的散列值,以尝试仅列出最高数字计数?
感谢之前的所有帖子,没有他们我不可能走到这一步。
干杯,
安迪
最佳答案
它可以稍微简化(我也做了一些文体上的改变以提高可读性):
my @data = (
[ qw /2012-02-21_09:43:43/ ],
[ qw /2012-02-21_09:43:43/ ]
);
my %counts;
foreach my $words (@data) {
my ($day, $hour) = ($words->[0] =~ /(\d{4}-\d{2}-\d{2})_(\d+):/ );
$counts{$day}->{$hour} += 1;
}
foreach my $day (keys %counts) {
foreach my $hour (keys %{ $counts{$day} }) {
print "Hour count for $day:$hour is: $counts{$day}->{$hour}\n";
}
}
对于您的查询而言,循环的工作部分是这样的:
my ($day, $hour) = ($words->[0] =~ /(\d{4}-\d{2}-\d{2})_(\d+):/ );
# You don't need minutes/seconds, so don't match them
# On the other hand, it's better to match YYYY/MM/DD explicitly!
# A regexp match in a list context will return a list of captures!
# e.g. ($1, $2, ...)
$counts{$day}->{$hour} += 1;
# You need to merely add 1 to a value. No need to push ones on a list.
# Please note that if the data is not guaranteed to be perfectly formatted,
# you need to defend against non-matches:
$counts{$day}->{$hour} += 1 if (defined $day && defined $hour);
下面是添加了注释的相同代码,阐明了我进行样式更改的原因:
my @data = ( # Don't use @a - variable name should have meanings
[ qw /2012-02-21_09:43:43/ ], # Not sure why you are using an array ref with
[ qw /2012-02-21_09:43:43/ ], # just 1 element, but let's pretend that is OK
);
my %counts;
foreach my $words (@data) { # Almost never rely on $_ - less readable
my ($day, $hour) = ($words->[0] =~ /(\d{4}-\d{2}-\d{2})_(\d+):/ ;
$counts{$day}->{$hour} += 1; # You can omit "->" but that's less readable
}
foreach my $day (keys %counts) { # Always localize your variable to the block they need
foreach my $hour (keys %{ $counts{$day} }) {
print "Hour count for $day:$hour is: $counts{$day}->{$hour}\n";
}
}
关于Perl 使用数组散列的散列计算每秒的日志条目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9376212/