【发布时间】:2020-06-24 23:54:22
【问题描述】:
我有一个旧的 perl 项目,一个事件日志的文本解析器,并收到了一个按事件 ID 对输出进行排序并删除重复事件的请求。因此解析器读取一个文本文件并将每个事件放入一个数组中。数组中的每个字段都包含一个带有多个键 -> 值对的散列。一个键称为序列,它包含事件的编号。我现在想根据每个数组字段的序列值对数组进行排序。其次,我想从数组中删除重复的相同序列号。
这里有一些我如何创建数组和散列的代码,以便您了解数据结构:
open (my $mel, "<", $in_filename) or die "\nFile '$in_filename' does not exist or is not readable.\n";
my $i=0;
my $eventcount = 0;
while (<$mel>) {
# Separate events by "Date/Time" :
if (/^$/) {
next;
}
if (/^Date\/Time:\s(.*)$/) {
if ($eventcount >0) {
$i++;
}
$eventcount++; # eventcount initialized with ‘0’
}
# Gathering information of the MEL event :
if (/^Date\/Time:\s(.*)$/) {$MEL[$i]{date} = $1; next;}
if (/^Sequence number:\s(\d+)$/) {$MEL[$i]{sequence} = $1; next;}
if (/^Event type:\s([0-9|a-f|A-F]{1,6})$/) {$MEL[$i]{type} = lc $1; next;}
if (/^Event category:\s(\w+)$/) {$MEL[$i]{category} = $1; next;}
if (/^Priority:\s(\w+)/) {$MEL[$i]{priority} = $1; next;}
if (/^Description:\s(.*)$/) {$MEL[$i]{description} = $1; next;}
if (/^Event specific codes:\s(.*)$/) {$MEL[$i]{code} = $1; next;}
if (/^Component location:\s(.*)$/) {$MEL[$i]{location} = $1; next;}
if (/^Logged by:\s.*(.)$/) {$MEL[$i]{logged_by} = $1; next;}
if (/^4[dD]\s45\s4[cC]\s48\s(\d\d)/) {$MEL[$i]{version} = hex $1;}
}
文本文件中的事件示例:
Date/Time: 2/3/20, 12:18:20 PM
Sequence number: 200 <==============
Event type: 5023
Event category: Command
Priority: Informational
Event needs attention: false
Event send alert: false
Event visibility: true
Description: Controller return status/function call for requested operation
Event specific codes: b8/1/0
Component type: Controller
Component location: Shelf 99, Bay A
Logged by: Controller in bay A
所以基本上我想根据散列中键的值对包含对散列的引用的数组进行排序。
其次,我想从数组中删除一个字段,此时键的值也存在于不同的数组字段中。
我希望有人能理解我的需要:-)
这可能吗?
【问题讨论】:
-
哪个字段是“事件ID”?
标签: arrays perl sorting hash unique