计算多个总和的百分比贡献

时间:2016-07-08 20:21:07

标签: perl

我在file.txt中有一个列表,其中有四个以制表符分隔的列

lineA   0.01  0.3  0
lineB   0     0.2  0.1
lineC   0.2   0    0.09

我想对每一行的3个数字求和(代码为@Aum的代码为lineA = 0.01 + 0.3 + 0),然后取每行的值并得出总和

0.31
0.3
0.29

total = 0.9 ($total)

然后为每一行创建一个百分比值:

percent lineA = ((0.31 * 100)/0.9)

我试过了:

open INFILE,  '<', "file.txt",    or die "cant open file $infile";
open OUTFILE, '>', "results.txt", or die "cant open";

while ( <INFILE> ) {

    my $line = $_;
    chomp( $line );

    if ( $line ) {

        my @columns = $_;

        foreach ( @columns ) {

            my ( $colum1, $colum2, $colum3, $colum4 ) = split( /\t/, $_ );

            if ( $percent ) {

                my @sum   = ( $colum2 + $colum3 + $colum4 );
                my $total = 0;

                foreach ( @sum ) {
                    $total += $_;
                    $percent = ( ( @sum * 100 ) / $total ) print OUTFILE "$percent\t$col1\n";
                }
            }


close INFILE;
close OUTFILE;

exit;

out file:

RESULTS.TXT

34.4 LineA
33.3 LineB
32.2 LineC

Well the code have fore option, but my problem in with the take the percent code. 

2 个答案:

答案 0 :(得分:4)

在计算百分比之前,您必须读入文件的所有行。为了保持输出行的顺序,我使用了一个哈希数组。请参阅perldsc

use warnings;
use strict;
use List::Util qw(sum);

my @lines;
my $total = 0;
while (<DATA>) {
    chomp;
    my ($line, @nums) = split;
    my $sum = sum(@nums);
    $total += $sum;
    push @lines, {line => $line, sum => $sum};
}

for my $lref (@lines) {
    my $percent = ($lref->{sum}*100)/$total;
    printf "%s %.1f\n", $lref->{line}, $percent;
}

__DATA__
lineA 0.01 0.3 0
lineB 0 0.2 0.1
lineC 0.2 0 0.09

输出:

lineA 34.4
lineB 33.3
lineC 32.2

答案 1 :(得分:0)

在计算百分比之前,您必须读入所有数据并累积数字字段的总计

use strict;
use warnings 'all';

use List::Util 'sum';

my $infile = 'file.txt';

my @data;

open my $fh,  '<', $infile or die qq{Unable to open "$infile" for input: $!};

while ( <$fh> ) {
    my ($id, @fields) = split;
    push @data, [ $id, sum(@fields) ];
}

my $total = sum( map $_->[1], @data );

printf "%s\t%.2f%%\n", $_->[0], $_->[1] * 100 / $total for @data;

输出

lineA   34.44%
lineB   33.33%
lineC   32.22%