2015-08-25 32 views
1

我有两个表。第一个是$sample,看起来像这样:使用多个值作为Perl散列中的键值

col1 col2 
A 1 
A 3 
A 4 
B 7 
... ... 

第二个是$exon,看起来像这样:

col1  col2 col3 col4 col5 
name1 A  1 100 200 
name2 A  2 300 400 
name3 A  3 500 600 
name4 A  4 700 800 

我想检查是否有来自$samplecol2col1col2之间的匹配col3 from exon

我通常在Perl中使用哈希值。我知道它是如何工作的,当你只是寻找两列之间的匹配。但我现在卡住了,因为来自两列的值应该匹配。这就是我对现在

my %hash =(); 
while(<$sample>){ 
    chomp; 
    my @cols = split(/\t/); 
    my $keyfield = $cols[0]; #col1 
    my $keyfield2 = $cols[1]; #col2 
    push @{ $hash{$keyfield}}, $keyfield2}; #this is probably not correct 
} 
    seek $exon,0,0; #cursor resetting 

while(<$exon>){ 
    chomp; 
    my @cols = split(/\t/); 
    my $keyfield = $cols[1]; #col2 
    my $keyfield2 = $cols[2]; #col3 
    if (exists($hash{$keyfield}) && exists($hash{$keyfield2})) { 
     print $output $cols[0], "\t", $cols[3], "\t", $cols[4], "\n"; 
} 
} 

回答

3

您应该使用COL2和COL3值的串联作为密钥你的可行性

my %hash =(); 
while(<$sample>){ 
    chomp; 
    my @cols = split(/\t/); 
    my $keyfield = $cols[0] #col1 
    my $keyfield2 = $cols[1] #col2 
    my $key = "$keyfield - $keyfield2"; 
    $hash{$key}=1; 
} 
    seek $exon,0,0 #cursor resetting 

while(<$exon>){ 
    chomp; 
    my @cols = split(/\t/); 
    my $keyfield = $cols[1]; #col2 
    my $keyfield2 = $cols[2]; #col3 
    my $key = "$keyfield - $keyfield2"; 
    if (exists($hash{$key}) { 
     print $output $cols[0], "\t", $cols[3], "\t", $cols[4], "\n"; 
    } 
} 
1

你可以把两个字段与您的哈希分隔符separarted键:

my @cols = split(/\t); 
my $keyfield = $cols[0]."--".$cols[1]; 
push @{ $hash{$keyfield}}, value}; 
+1

@Downvoter:请解释。 – Jens

相关问题