http://www.perlmonks.org?node_id=598289


in reply to Using map and grep to Sort one list using another list

I think the (ahem!) key here is to convert your @keys array into a hash to make it easier to check for the existance of keys in your data line.

my @keys = ( qw|a e i o u| ); my %keys = map { $_ => 1 } @keys; my @records = ( '1|2|3|d|4', '1|2|3|a|4', '1|2|3|d|4', '1|2|3|o|4', ); my @final; my @result; foreach (@records) { if ($keys{ (split /\|/)[3] }) { push @result, $_; } } print map { "$_\n" } @result;