Re: [問題] 讀取檔案的內容

看板Perl作者 ( only you )時間12年前 (2012/11/13 23:45), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串9/10 (看更多)
您好 因為最近還有計算總量的需求 所以我將之前的輸出到檔案 接著再用另一隻perl去計算總量 以下為我的程式碼 my $number; my $total; open (RESULT , "<". $result_file); while(<RESULT>) { if (/Number:\s+(\d+),\s+Kind\s+(\d+),\s+quantity:\s+(\d+)/) { $number = $1; my $qty = $3; $total->{ $number } += $qty ; } print "代號: $number, total: $total->{ $number }\n"; } close (RESULT); ----- 讀入的result_file格式如下 Number: 1, Kind: 0, quantity: 12 Number: 1, Kind: 1, quantity: 9 Number: 2, Kind: 0, quantity: 23 Number: 2, Kind: 1, quantity: 1 Number: 3, Kind: 0, quantity: 25 Number: 3, Kind: 2, quantity: 11 Number: 4, Kind: 0, quantity: 9 Number: 4, Kind: 1, quantity: 1 ----- 輸出如下 代號: 1, total: 12 代號: 1, total: 21 代號: 2, total: 23 代號: 2, total: 24 代號: 3, total: 25 代號: 3, total: 36 代號: 4, total: 9 代號: 4, total: 10 但是我只想要輸出 代號: 1, total: 21 代號: 2, total: 24 代號: 3, total: 36 代號: 4, total: 10 請問我的寫法要怎麼改比較好呢? 感謝~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.253.124.20
文章代碼(AID): #1GeciGFJ (Perl)
文章代碼(AID): #1GeciGFJ (Perl)