Re: [請益] Hash reference應用(soved)
※ 引述《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.
--
The elevator to success is out of order. You'll have to use the stairs one
step at a time.
像搭電梯般快速成功是不切實際的。你必需沿著樓梯,一步一階的往上爬。
Joe Girard
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.246.134 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Perl/M.1587293768.A.2C7.html
→
04/19 19:30,
4年前
, 1F
04/19 19:30, 1F
Perl 近期熱門文章
PTT數位生活區 即時熱門文章