从两个不同的文件-Perl中平方并乘以两行

时间:2014-05-05 12:11:06

标签: perl file row multiplying

如何使用Perl对两个不同文件中的两行进行平方然后相乘? 这是我的两个文件:

file1:
ID_A    AAA BBB CCC DDD FFF LLL 
ID_B    F   F   L   L   L   L   
SCORE    0.750   0.000   0.857   0.857   1.286   0.000  

file2:
ID_A    AAA BBB CCC DDD FFF LLL 
ID_B    F   F   L   L   L   L   
SCORE    1.333   0.667   1.636   0.000   1.091   0.545    

我想在两个文件的“SCORE”行中对每个值进行平方,然后将它们相乘,比如; file1 SCORE AAA到LLL,带有相应的文件2 SCORE AAA到LLL:

 ie (0.750 * 0.750) * (1.333 * 1.333)
    (0.000 * 0.000) * (0.667 * 0.667)
     and so on... 

请帮我在Perl中编写这个程序。

1 个答案:

答案 0 :(得分:0)

记住数组中的方块,当你遇到它们时乘以下一个方块:

#!/usr/bin/perl
use warnings;
use strict;

my @results;
while (<>) {
    if (/^SCORE/) {
        my @scores = split;
        shift @scores;
        $_ *= $_ for @scores;
        if (@results) {
            @results = map { $_ * shift @scores } @results;
        } else {
            @results = @scores;
        }
    }
}

print "@results\n";

保存到mutliply.pl,以multiply.pl file1 file2运行。

输出:

0.9995000625 0 1.965749810704 0 1.968481956676 0