检查一个哈希的键是否与另一个哈希的值相同?

时间:2013-06-12 03:25:54

标签: perl hash

我试图看到散列%pc中的值位于散列%fasta中的键中,然后打印密钥和%fasta的值(如果是)。

到目前为止,我有脚本:

#!/usr/bin/perl
use warnings;
use strict;
use Bio::SearchIO;
use Bio::SeqIO;

my %pc;
my %fasta;

#get list of core protein clusters
my $file1 = $ARGV[0];
open (INPUT1, $file1) or die "Could not open $file1\n";
while (my $line1 = <INPUT1>) {
    chomp $line1;
    my @spl = split (/\|/, $line1);
    my $id = $spl[0];
    my $gene = $spl[1];
    $pc{$id}=$gene;
}


my @files = glob("*_PC.fa");
foreach my $fil (@files) {
    my $seqio = Bio::SeqIO->new(-format => 'fasta', -file   => $fil);
    my $file = $fil;
    $file =~ /^(.+)_PC.fa/;
    my $outfile = $1."_core.fa";
    open (OUTFILE,'>', $outfile) or die "Could not open $outfile \n";
    while (my $seqobj = $seqio->next_seq) {
        my $seqid = $seqobj->display_id;
        my $nuc = $seqobj->seq();
        $fasta{$seqid}=$nuc;
        foreach my $key (keys %fasta) {
            my @spl2 = split (/\|/,$key);
            my $fasta_gene = $spl2[1];
            if ($fasta_gene eq (keys %fasta)) {
                print OUTFILE ">$key\n$fasta{$key}\n";
            }
        }
    }
    close OUTFILE;    
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

如果%fasta中的所有值都作为[{1}}

中的键存在,则会打印

%pc

%fasta