Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: General Advice

by hdb (Parson)
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
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 meditating upon the Monastery: (7)
As of 2014-07-14 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (257 votes), past polls