my $gene_to_find = 'NP_012'; my $matching_taxon; TAXON: for my $taxon (keys %gg) { for my $gene ( @{ $gg{$taxon} } ) { if ($gene eq $gene_to_find) { $matching_taxon = $taxon; last TAXON; } } } if (defined($matching_taxon)) { print("Gene $gene_to_find found in taxon $matching_taxon.\n"); } else { print("Gene $gene_to_find not found in any taxon.\n"); } #### my $gene_to_find = 'NP_012'; my @matching_taxons; for my $taxon (keys %gg) { for my $gene ( @{ $gg{$taxon} } ) { if ($gene eq $gene_to_find) { push @matching_taxons, $taxon; } } } if (@matching_taxons) { print("Gene $gene_to_find found in taxons @matching_taxons.\n"); } else { print("Gene $gene_to_find not found in any taxon.\n"); }