perl中同时读取两个文件并进行对应计算

前端之家收集整理的这篇文章主要介绍了perl中同时读取两个文件并进行对应计算前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

用句柄读取大文件进数组是很占内存的事,所以,一边读数据,一边处理数据,处理完之后就直接输出文件,这是个很靠谱的想法,尤其是处理特大数据的时候,这个想法尤其珍贵。下面是具体例子:

输入文件1:

31      0
32      0
33      0
34      0
35      0
36      0
37      0
38      0
39      0
40      0
41      0
42      0.95
43      0
44      0

输入文件2:

92      0
93      0
94      0
95      0
96      0
97      0
98      0
99      0
100     0
101     0
102     0
103     0
104     0
105     0
106     0
107     0
108     0
109     0
110     0.138888888888889
111     0
112     0
113     0
114     0
115     0
116     0
117     0
118     0

处理代码

#!/usr/bin/perl

use strict;
use warnings;

my $pb_file = shift;    #read file of polar bear
my $bb_file = shift;    #read file of blown bear

my $n1 = 18;
my $n2 = 10;

open (AA,"$pb_file") or die "$!";
open (BB,"$bb_file") or die "$!";
open (CC,">a_ab.txt") or die "$!";

my $aa;
my $bb = <BB>;

while( $aa = <AA> ){
        my @pb = split /\s+/,$aa;
        my @bb = split /\s+/,$bb;

        while ($pb[0] ne $bb[0]){
                if ( $pb[0] > $bb[0]){
                        $bb = <BB>;
                        @bb = split /\s+/,$bb;
                }else{
                        $aa = <AA>;
                        @pb = split /\s+/,$aa;
                }
        }

        my $p12_2 = ($pb[1] - $bb[1])*($pb[1] - $bb[1]);

        my $alpha1 = 2 * $pb[1] * (1 - $pb[1]);
        my $alpha2 = 2 * $bb[1] * (1 - $bb[1]);

        my $a = $p12_2 - ($n1+$n2)*($n1*$alpha1 + $n2*$alpha2) / 4*$n1*$n2*($n1+$n2-1);
        my $b = $p12_2 + (4*$n1*$n2-$n1-$n2)*($n1*$alpha1 + $n2*$alpha2) / 4*$n1*$n2*($n1+$n2-1);
        my $fst;

        if ($a == 0){
                $fst = 0;
        }else{
                $fst = $a / ($a + $b);
        }

        print CC "$bb[0]\t$a\t$b\t$fst\t\n";
}


求出数据之后可以作图...........待续

猜你在找的Perl相关文章