【发布时间】:2014-06-18 13:48:18
【问题描述】:
我有一个白天发生警报的数据文件。格式看起来像
2014/04/27-23:42:22.177742- Alarm1
2014/04/27-23:42:22.177744- Alarm2
2014/04/27-23:42:22.177747- Alarm3
2014/04/27-23:42:22.177749- Alarm1
现在我无法猜测何时会出现任何警报。这取决于系统。我所做的是将警报数据(例如 Alarm1)插入 2D 散列。我每次都花 5 分钟的时间寻找在 5 分钟内出现的警报。每次发现新警报时,我都会将该值添加到哈希中。如果出现重复(如上面的 Alarm1),我只需将值加 1。所以最后它会给我一个哈希值,其中包含警报名称和它在 5 分钟内出现的时间。 接下来我将开始处理接下来的 5 分钟。
我一整天都在处理它,所以有可能在早上 10 点开始出现 1 个警报,所以这将是哈希的一个新条目。现在,当我尝试最终将值打印到 CSV 时,它一团糟。完全没有意义。我期望的是一个看起来像
的 csvName,00:00,00:05,00:10,
Alarm1,2,5,2,7,
Alarm2,4,7,3,6
Alarm3,6,1,6,3
...
我的代码是:
use Time::Local;
use POSIX 'strftime';
use Data::Dumper;
my %outputHash= ();
$curr = timelocal(0, 0, 0, (split /\//, $ARGV[0])[1], (split /\//, $ARGV[0])[0]-1, (split /\//, $ARGV[0])[-1]);
$currentTime = strftime "%Y/%m/%d-%H:%M:%S", localtime($curr);
for ($count = 1; $count <= 288; $count++) { #there are 288 '5 minutes' in a day.
$curr += 300;
$nextTime = strftime "%Y/%m/%d-%H:%M:%S", localtime($curr);
$cmd = "awk '\$0>=from&&\$0<=to' from=\"$currentTime\" to=\"$nextTime\" Output.txt";
my $dataChunk = qx($cmd);
my @lines = split /[\n]+/, $dataChunk;
foreach my $line (@lines) {
chomp;
$timeStamp1 = substr($line,21,6);
#print "\n$timeStamp1\n$error\n";
if ($timeStamp1 != $timeStamp2){
$outputHash{$error}{$count} = $outputHash{$error}{$count} + 1;
}
$ind = index($line,'- ') + 2;
$len = length($line) - $ind;
$error = substr($line,$ind, $len);
$timeStamp2 = $timeStamp1;
}
$currentTime = $nextTime;
# if ($count>3){$count=300;}
}
`>/tmp/report.txt`;
open (MYFILE, '>>/tmp/report.txt');
my @outputArray = ();
my $flag =1;
foreach my $error (sort keys %outputHash)
{
print MYFILE "$error,";
#$outputArray[$flag][0] = $error;
for ($count=1,$count <= 288, $count++)
{
print MYFILE "$outputHash{$error}{$count},";
#$outputArray[$flag][$count] = int($outputHash{$error}{$count});
}
$flag += 1;print MYFILE "\n";
}
close (MYFILE);
#print Dumper(\@outputArray);
exit;
我的简化显示如下所示。其随意性的原因是因为警报 1 仅在“第 2 个”5 分钟间隔内发生,警报 2 仅在第 1 个发生,警报 3 在我们监测的 4 个连续 5 分钟间隔内发生。
'Alarm1{
'2' => '5'
},
'Alarm2{
'1' => '1'
},
'Alarm3
'4' => '1',
'1' => '2',
'3' => '1',
'2' => '1'
},
【问题讨论】:
-
你能发布你的哈希结构吗?
-
刚刚编辑它以包含哈希
标签: arrays perl parsing csv hash