作者hws110 ( only you )
看板Perl
标题Re: [问题] 读取档案的内容
时间Tue Nov 13 23:45:18 2012
您好
因为最近还有计算总量的需求
所以我将之前的输出到档案
接着再用另一只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