我一直在尝试查找在大文件的两列(列 a 和列 b)之间匹配的值,并打印公共值以及相应的列 d。我一直在通过哈希进行交互来做到这一点,但是,因为文件太大,没有足够的内存来生成输出文件。有没有其他方法可以使用更少的内存资源来做同样的事情。
任何帮助深表感谢。
到目前为止,我编写的脚本如下:
#!usr/bin/perl
use warnings;
use strict;
open (FILE1, "<input.txt") || die "$!\n Couldn't open input.txt\n";
open (Output, ">output.txt")||die "Can't Open output.txt ";
my $hash1={};
my $hash2={};
while (<FILE1>) {
chomp (my $line=$_);
my ($a, $b, $c, $d) = split (/\t/, $line);
if ($a) {
$hash1->{$a}{info1} = "$d"; #original_ID-> YOB
}
if ($b) {
$hash2->{$b}{info2} = "$a"; #original_ID-> sire
}
foreach my $key (keys %$hash2) {
if (exists $hash1{$a}) {
$info1 = $hash1->{$a}->{info1};
print "$a\t$info1\n";
}
}
}
close FILE1;
close Output;
print "Done\n";
为了澄清,输入文件是一个大的谱系文件。一个例子是:
1 2 3 1977
2 4 5 1944
3 4 5 1950
4 5 6 1930
5 7 6 1928
输出文件的一个例子是:
2 1944
4 1950
5 1928