my %HoAncestors; foreach my $qu (keys %HoTxHits) { # go through each taxid, get ancestors, then find the intersection foreach my $taxid (@{$HoTxHits{$qu}}){ my $queryTaxid=$taxid; while ((exists $HoPar{$queryTaxid}) && ($HoPar{$queryTaxid} != 1)) { my $parent=$HoPar{$queryTaxid}; push (@{$HoAncestors{$taxid}},$parent); $queryTaxid=$parent; } } $lcm=List::Compare=>new(\ @intersection=$lcm->get_intersection; } }