繁体   English   中英

Perl:如何比较一个哈希中的键和另一个哈希中的值?

[英]Perl: How to compare key from one hash with value from another hash?

好的,所以我有2个散列,如下所示:

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

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

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

我可以轻松地基于键比较hash2hash3 ,但是我还需要hash1信息, 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";
        }
    }

遍历键,而不是值。

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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM