Re: [問題] 取百分比問題

看板Perl作者 (^_^)時間17年前 (2007/12/06 19:45), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/4 (看更多)
這是我檔案的部分內容....資料量大概是幾千筆...假設兩個檔案都一模一樣 ("ABC": 32'sh00000001) = 16 : 0.27 ("ABC": 32'sh00000002) = 24 : 0.41 ("ABC": 32'sh00000003) = 0 : 0.00 ("ABC": 32'sh00000004) = 0 : 0.00 ("ABC": 32'sh00000005) = 8 : 0.14 ("ABC": 32'sh00000006) = 0 : 0.00 ("ABC": 32'sh00000007) = 12 : 0.20 ("ABC": 32'sh00000008) = 0 : 0.00 ("ABC": 32'sh00000009) = 12 : 0.20 ("ABC": 32'sh0000000a) = 12 : 0.20 (default) = 0 : 0.00 16那一欄代表的是次數....0.27那一欄代表比例 我的程式為 $input_file = $ARGV[0]; $output_file = $ARGV[1]; $output_file_not_exst = 0; open (INPUT_H, "$input_file") || die ("Error! can't open $input_file for reading."); open (OUTPUT_H, "$output_file") || die ("Error! can't open $output_file for reading."); while($_ = <INPUT_H>) { push(@first_trigger, $_); } while($_ = <OUTPUT_H>) { push(@second_trigger, $_); } for ($j=0;$j<@second_trigger;$j++) { @t = split ' = ', $second_trigger[$j]; $second_bin = $t[0]; @t2 = split ' : ', $t[1]; $second_count = $t2[0]; for ($k=0;$k<@first_trigger;$k++) { @m = split ' = ', $first_trigger[$k]; $first_bin = $m[0]; @m2 = split ' : ', $m[1]; $first_count = $m2[0]; if ( $second_bin =~ m/\(/ ) { if ($second_bin eq $first_bin) { $k=9000; $second_count = $second_count + $first_count; $sum += $second_count; $second_trigger[$j] = "$second_bin = $second_count : 0.00"; } # match } # bin match } # for #print OUTFILE_H $second_trigger[$j]; } 這個程式現在只能處理次數相加....我想要在最後把比例重新算出來.. $sum 是次數相加....比例的算法是把 每一列的次數除以 sum(最後一筆) 但是之前把比例的部分都填零(0.00)... 想請問要怎麼改才能把最後算出來的比例都寫回output file裡??? 謝謝~~~ ※ 引述《cutecpu (可愛中央處理器)》之銘言: : #!/usr/bin/perl : open F1,"1.txt"; : open F2,"2.txt"; : while(<F1>=~/\d+/){ : $tmp=$&; : <F2>=~/\d+/; : $tmp+=$&; : $sum+=$tmp; : push @a,$tmp; : } : print "$_:",sprintf("%.2f",$_/$sum),$/ for(@a); : ※ 引述《iswearwang (^_^)》之銘言: : : 假設我有兩個檔案 1.txt , 2.txt : : 其內容皆為 : : 1:0.1 : : 2.0.2 : : 3:0.3 : : 4:0.4 : : 5:0.5 : : 6:0.6 : : 7:0.7 : : 8:0.8 : : 9:0.9 : : 0:0 : : . : : . : : . : : 左邊為次數,右邊為百分比.....我現在把兩個檔案作合併...次數可以直接相加 : : 可是百分比要重算...我是一列一列讀進來...請問要怎麼抓sum次數的最後一筆 : : 我要直接把次數除以sum 取百分比....另外 perl的除法 : : 我寫 $coverage = $num/$sum.... : : compile會有 illeage division by zero的問題 : : 不曉得是哪裡出錯了...?? : : 謝謝回應 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 211.75.133.138
文章代碼(AID): #17L-3nPf (Perl)
討論串 (同標題文章)
文章代碼(AID): #17L-3nPf (Perl)