search(["bobcat", "boomerang", "beer", "bat"], ["-bo"], ["b"]); sub search { my ($data, $negative_criteria, $positive_criteria = @_; my $temp_array; my $results; my $match_regex = join "|", @$positive_criteria; # remove trailing "-" which denotes negated criteria my $negative_regex = [map { s/^-//, $_ } @$negative_criteria]; $negative_regex = join "|", @$negative_criteria; # filter through all matches to array foreach(@$data) { if ($_ =~ m/$match_regex/) { push @$temp_array, $_; } } # remove negative criteria and build new array foreach(@$temp_array) { if ($_ !~ m/$negative_regex/) { push @$results, $_; } } my $count = 0; foreach(@$results) { print "$count - $_\n"; $count++; } return $results; } Output: 0 - beer 1 - bat