Perl:如何比较一个哈希的密钥和另一个哈希的值?

时间:2014-07-28 18:45:30

标签: perl hash key compare

好的,我有2个哈希,看起来像:

%hash1=(name1 => seq1,
        name2 => seq2);

%hash2=(number1 => name1,
        number2 => name2);

%hash3=(number1 => fam1,
        number2 => fam2);

我可以根据键轻松比较hash2hash3,但我需要hash1中的信息以及添加到另一个输出文件中。对不起,如果这听起来太模糊了。但是,让我知道如何将一个哈希中的键与另一个哈希中的值进行比较。

代码看起来像这样:%tablehash是hash2,%newhash是hash1

for my $new_NH(values %tablehash){
    if(exists($newhash{$new_NH})){
        my $taxa_H = $new_NH; #this works
        my $seq_H = $value_NH; #this works too
        my $gi_H=  $somekey;  #this should be the key of table hash, don't know how to access it, I have named it somekey for posting for now
        my $fam_H= $somevalue ;   #this should be the value from another hash3
        print OUTTRIMMED ">fam_H"."_$taxa_H"."_$gi_H\n$seq_H\n\n";
        }
    }

1 个答案:

答案 0 :(得分:0)

迭代键,而不是值。

for my $gi_H (keys(%tablehash)) {
   my $taxa_H = $tablehash{$gi_H};
   my $fam_H  = $hash3{$gi_H};

   next if !exists($newhash{$taxa_H});

   my $seq_H = $newhash{$taxa_H};

   printf(OUTTRIMMED ">%s_%s_%s\n%s\n\n",
      $fam_H, $taxa_H, $gi_H, $seq_H);
}
相关问题