簡體   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