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

Re: Need RegExp help - doing an AND match - use grep instead

by imp (Priest)
on Jul 01, 2007 at 13:33 UTC ( #624298=note: print w/ replies, xml ) Need Help??


in reply to Need RegExp help - doing an AND match

Is there a reason why you want to do this with a regex? It isn't really the right tool for the job.

I would probably just use grep to get a list of the missing items.

my @words = qw( foo bar ); while (my $line = <DATA>) { chomp $line; my @missing = grep {$line !~ $_} @words; printf "line: %-10s; missing: %s\n", $line, join ',', @missing; } __DATA__ foo bar foo bar
Or use List::Util's first, which will abort the search once one is missing.
use List::Util qw( first ); my @words = qw( foo bar ); while (my $line = <DATA>) { chomp $line; my $missing = first {$line !~ $_} @words; printf "line: %-10s; missing: %s\n", $line, $missing; } __DATA__ foo bar foo bar
Note that this will only return the first missing item, and if you're searching for something like "0" then you should check whether $has_missing is defined.


Comment on Re: Need RegExp help - doing an AND match - use grep instead
Select or Download Code
Re^2: Need RegExp help - doing an AND match - use grep instead
by Anonymous Monk on Jul 01, 2007 at 13:51 UTC
    Ah, fantastic, hadn't even thought of using grep. Thanks very much, rather an embarrassing first post. No, there wasn't any special need for Regexp other than trying to learn a bit more about them. My code now looks like this and works:
    #@words is a bunch of words , $line is the line to search if(!grep{$line !~ $_} @words) { #success }
    (I didn't actually need the missing ones.) Thanks a lot, imp!
Re^2: Need RegExp help - doing an AND match - use grep instead
by Anonymous Monk on Jul 01, 2007 at 21:05 UTC
    Apologies all. I've re-read all posts here and List::Util first does the trick. Thanks to all who replied!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-09-20 16:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls