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