作者saka12 (saka)
看板Perl
标题Re: [问题] 如何从档案里取数字并相加
时间Fri Feb 10 12:47:45 2012
※ 引述《saka12 (saka)》之铭言:
: 大家好:
: 我手边有一个档案 input.txt 如下
: the 77 survey: 77 out of 100 people will not pick the number 1 as the first choice.
: A stitch in time saves 9 but only if you have exactly 9.
: 52 trombones were in 5 marching bands in the big parade of 52 and 52 people attended.
: 要取出每行的数字,去除重复的数字,并相加
: 77+100+1+9+52+5= 274
: 我已经把每行的数字取到阵列中,可是我不知道删掉重复的数字
: 有想过用hash,可我怎麽写都写不出来,请大家给我一点提示
: 谢谢
读档案那段跳过
for my $new(@data)
{
my @num = $new =~ /\d+/g;
for my $out(@num) {
if (!$saw{$out}) {
print "$out ";
$saw{$out} = 1;
$partialSum .= "$out+";
$sum += $out;
}
}
print "$partialSum=$sum";
想请问以下这段要怎麽解读
if (!$saw{$out}) {
print "$out ";
$saw{$out} = 1;
}
还有以下这段应该也可以删除重复字元,可是我看不懂,我卡在 !$ 这里
my @unique = grep { ! $seen{ $_ }++ } @array;
附上好用的资料
http://ppt.cc/q0uS
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 24.130.53.186
1F:推 k75715:预设值是undef 02/11 14:53