【发布时间】:2012-01-31 10:29:00
【问题描述】:
我已经问过这个问题了(不好意思再问,这次不一样也很难)但是我尝试了很多但没有达到结果。
我有 2 个大文件(制表符分隔)。
第一个文件 ->
Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 101_#2 1 H F0 263 278 2 1.5 102_#1 1 6 F1 766 781 1 1.0 103_#1 2 15 V1 526 581 1 0.0 103_#1 2 9 V2 124 134 1 1.3 104_#1 1 12 V3 137 172 1 1.0 105_#1 1 17 F2 766 771 1 1.0
第二个文件->
Col1 Col2 Col3 Col4 97486 H 262 279 67486 9 118 119 87486 9 183 185 248233 9 124 134
如果 col3 值/字符(文件 1)和 col2 值/字符(文件 2)相同,然后将文件 1 的 col5 和 col6(如范围值)与文件 2 的 col3 和 col4 进行比较,如果文件范围1 存在于文件 2 中,然后返回该行(来自 file1)并在输出中添加来自 file2 的额外列 1。
预期输出 ->
Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 Col9 101_#2 1 H F0 263 278 2 1.5 97486 103_#1 2 9 V2 124 134 1 1.3 248233
到目前为止,我已经尝试了一些带有哈希的东西->
@ARGV or die "No input file specified";
open my $first, '<',$ARGV[0] or die "Unable to open input file: $!";
open my $second,'<', $ARGV[1] or die "Unable to open input file: $!";
print scalar (<$first>);
while(<$second>){
chomp;
@line=split /\s+/;
$hash{$line[2]}=$line[3];
}
while (<$first>) {
@cols = split /\s+/;
$p1 = $cols[4];
$p2 = $cols[5];
foreach $key (sort keys %hash){
if ($p1>= "$key"){
if ($p2<=$hash{$key})
{
print join("\t",@cols),"\n";
}
}
else{ next; }
}
}
但是在上面的代码中没有比较 col3 值/字符(文件 1)和 col2 值/字符(文件 2)。 但这也需要大量时间和内存。任何人都可以建议我如何使用散列或散列的散列使其快速。非常感谢。
大家好,
非常感谢您的帮助。我为自己的问题找到了一种有效的方法。
@ARGV or die "No input file specified";
open $first, '<',$ARGV[0] or die "Unable to open input file: $!";
open $second,'<', $ARGV[1] or die "Unable to open input file: $!";
print scalar (<$first>);
while(<$second>){
chomp;
@line=split /\s+/;
$hash{$line[1]}{$line[2]}{$line[3]}= $line[0];
}
while (<$first>) {
@cols = split /\s+/;
foreach $key1 (sort keys %hash) {
foreach $key2 (sort keys %{$hash{$key1}}) {
foreach $key3 (sort keys %{$hash{$key1}{$key2}}) {
if (($cols[2] eq $key1) && ($cols[4]>=$key2) && ($cols[5]<=$key3)){
print join("\t",@cols),"\t",$hash{$key1}{$key2}{$key3},"\n";
}
last;
}
}
}
}
对吗?
【问题讨论】:
标签: perl