作者iswearwang (^_^)
看板Perl
标题Re: [问题] 取百分比问题
时间Thu Dec 6 19:45:52 2007
这是我档案的部分内容....资料量大概是几千笔...假设两个档案都一模一样
("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