I'm writing a search to go through an array of data, I'm looking for any suggestions on algorithms, structuring, or any language advice to make my code better. Also, what is a better way to build my regexes?
Thanks!
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
In reply to General Advice by yoda54
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |