Re: [問題] 如何從檔案裡取數字並相加

看板Perl作者 (saka)時間13年前 (2012/02/10 12:47), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串3/3 (看更多)
※ 引述《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

02/11 14:53, , 1F
預設值是undef
02/11 14:53, 1F
文章代碼(AID): #1FDA5qTW (Perl)
文章代碼(AID): #1FDA5qTW (Perl)