Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: grep for lines containg two variables

by pileofrogs (Priest)
on Dec 07, 2005 at 18:56 UTC ( [id://514976]=note: print w/replies, xml ) Need Help??


in reply to grep for lines containg two variables

Does one string always preceed the other string? Or can they be in any order?

If I've got my head screwed on correctly (always a doubtful proposition,) some of the solutions above will work in both cases and other solutions will only work if $string1 appears before $string2.

From ikegami's post:

Works in any order

@interesting_lines = grep /$string1/, grep /$string2/, @log;

Works in any order

@interesting_lines = grep /$string1/ && /$string2/, @log;

Only works if $string1 preceeds $string2

@interesting_lines = grep /^(?=.*$string1)(?=.*$string2)/, @log;

Or am I a total moron?

Replies are listed 'Best First'.
Re^2: grep for lines containg two variables
by Roy Johnson (Monsignor) on Dec 07, 2005 at 19:02 UTC
    Because it's done with lookaheads, the last example works with strings in either order. That is, in fact, why it is done with lookaheads. In my mind, lookaheads like this are the "and" that complements alternation's "or" in regexen.

    Caution: Contents may have been coded under pressure.
      Woah! Thanks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-16 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found