看板 Perl 關於我們 聯絡資訊
※ 引述《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,可我怎麼寫都寫不出來,請大家給我一點提示 : 謝謝 非常直白的寫法 $string="your stirng"; @nums = ($string =~ /\d+/g); %unique = map { $_, 1} @nums; $sum += $_ for keys %unique; print $sum; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.109.22.80
scwg: for 那行只需要 $sum = keys %unique 02/09 12:58
scwg: Oops, 想錯了, 原 po 是對的 orz 02/09 12:59