Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2015-10-10 13:19 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (257 votes), past polls