Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: General Advice

by hdb (Monsignor)
on Jun 27, 2013 at 07:56 UTC ( [id://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";

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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1040947]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-19 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found