#assumes %the_hash{'this_id'} = { key1=>'quux', key4=>12 } and so on my @selected_id_keys = grep { ( exists $the_hash{$_}{'key1'} and $the_hash{$_}{'key1'} eq 'foo') and ( exists $the_hash{$_}{'key2'} and $the_hash{$_}{'key2'} eq 'bar') or ( exists $the_hash{$_}{'key4'} and $the_hash{$_}{'key4'} < 10) } keys %the_hash;