Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: General Advice

by hdb (Prior)
on Jun 27, 2013 at 07:56 UTC ( #1040947=note: print w/ replies, xml ) Need Help??


in reply to General Advice

I recommend using grep in this case. One thing to observe on everything proposed so far: the use of s/// within sub search has side effects outside of the the sub as it modifies the input! If it is an anonymous array reference it does not make any difference, but otherwise it might create problems later. See the difference between the line commented out below and the following one.

use strict; use warnings; sub search { my ( $data, $neg, $pos ) = @_; my $regp = join "|", @$pos; # my $regn = join "|", map { s/^-//, $_ } @$neg; # warning: si +de effects outside of sub search! my $regn = join "|", map { /^-?(.*)/ } @$neg; # no side effect +s my @results = grep { /$regp/ && !/$regn/ } @$data; return \@results; } my $neg = ["-bo"]; my $results = search(["bobcat", "boomerang", "beer", "bat", "aaa" ], $ +neg, ["b"]); print "@$results\n"; print "@$neg\n";
Comment on Re: General Advice
Select or Download Code
Replies are listed 'Best First'.
Re^2: General Advice
by yoda54 (Monk) on Jun 27, 2013 at 20:20 UTC
    Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1040947]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2016-04-30 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    :nehw tseb si esrever ni gnitirW







    Results (441 votes). Check out past polls.