※ 引述《nash7555 (Caspar)》之铭言:
: Hi 各位前辈/先进,
: 小弟想用perl做log比较差异, 由于两份log资料内容举例如下:
: Warning (00001): Can't analyze file - no such file exists
: Warning (00100): warning at zzzzzzzzzzzzzzzzzz.v
: Warning (00100): warning at abcdef_g1h2_ev5_dataflow.vow.v
: Warning (00100): warning at aaaaaaaaaaaaa.v
: ...
: ...
: ...
: 两份log都有2xxxx行, 尤其中间有些讯息需要bypass, 所以逐行比较会跑好超过10小时。
: 我想利用hash = {key, value}来做,变成为:
: hash = {
: 00001, Warning (00001): Can't analyze file - no such file exists
: 00002, Warning (00002): ????????????????????????????????
: ...
: ...
: 00100, Warning (00100): warning at zzzzzzzzzzzzzzzzzz.v
: ...
: ...
: }
: 如上述, 我比较的时候可以先抓key缩小范围再比较, 这样效率会提升。
: 但是如刚刚举例的log内容, 同一个warning number, 其实有非常多项, 因为描述会不同。
: 所以单纯的hash无法满足我的应用, 因为一个warning number只对应一个value。
: 我想像是希望能做到如下:
: hash = {
: 00001, Warning (00001): Can't analyze file - no such file exists
: 00002, Warning (00002): ????????????????????????????????
: ...
: ...
: 00100, Warning (00100): warning at zzzzzzzzzzzzzzzzzz.v
: 00100. Warning (00100): warning at abcdef_g1h2_ev5_dataflow.vow.v
: 00100, Warning (00100): warning at aaaaaaaaaaaaa.v
: ...
: ...
: }
: 网络上查, 说要用reference才可以。但目前网络上资料有看没有懂, 请问各位高手能
: 不能指点一下, code该怎么写, 谢谢~
: 现在程式码大概如下:
: %warnings_line_ori = ();
: while(<>){
: ...
: ...
: $warnings_line_ori{$warning_no} = $line;
: ...
: ...
: }
我研究出来了, 感恩~
实验程式如下:
#!/usr/bin/perl -w
my %arrayScore = ();
$case1 = 0;
$arrayScore{'Tom'}->[0] = $case1;
$arrayScore{'Tony'}->[0] = 80;
$arrayScore{'Tony'}->[1] = 85;
$arrayScore{'Cindy'}->[0] = 95;
$arrayScore{'Cindy'}->[1] = 90;
$arrayScore{'Cindy'}->[2] = 100;
print "Tom $arrayScore{'Tom'}->[0]\n";
print "Tony $arrayScore{'Tony'}->[0], $arrayScore{'Tony'}->[1]\n";
print "Cindy $arrayScore{'Cindy'}->[0], $arrayScore{'Cindy'}->[1],
$arrayScore{'Cindy'}->[2]\n";
print
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
print keys %arrayScore;
print "\n";
print @{$arrayScore{'Tony'}};
print
"\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
@test = @{$arrayScore{'Cindy'}};
while (each @test){
my $no =$_;
print $no.", $test[$no]\n";
}
print "Presence.\n" if (exists($arrayScore{'Tony'}));
if (exists($arrayScore{'Jones'})){
print "Presence.\n";
}else{
print "Does not exist.\n";
}
显示如下:
Tom 0
Tony 80, 85
Cindy 95, 90, 100
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CindyTonyTom
8085
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0, 95
1, 90
2, 100
Presence.
Does not exist.