yoda54 has asked for the wisdom of the Perl Monks concerning the following question:
Monks,
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: General Advice
by hbm (Hermit) on Jun 27, 2013 at 01:31 UTC | |
by Anonymous Monk on Jun 27, 2013 at 02:53 UTC | |
by yoda54 (Monk) on Jun 27, 2013 at 01:40 UTC | |
Re: General Advice
by NetWallah (Canon) on Jun 27, 2013 at 04:57 UTC | |
by yoda54 (Monk) on Jun 27, 2013 at 20:18 UTC | |
Re: General Advice
by kcott (Archbishop) on Jun 27, 2013 at 06:51 UTC | |
by yoda54 (Monk) on Jun 27, 2013 at 20:19 UTC | |
Re: General Advice
by hdb (Monsignor) on Jun 27, 2013 at 07:56 UTC | |
by yoda54 (Monk) on Jun 27, 2013 at 20:20 UTC | |
Re: General Advice
by clueless newbie (Curate) on Jun 27, 2013 at 11:56 UTC |
Back to
Seekers of Perl Wisdom