Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
By "reverse grep", I take it that you mean you want to loop over a list of regexes with one string, rather than loop over a list of strings with one regex. As far as Perl is concerned, these are the same.

Your difficulty comes from thinking of Perl's grep as being the same as the command line grep. This is not the case. In Perl's grep, the conditional is not necessarily a pattern match; it is any arbitrary Perl expression. For example, you could do something like this: @results = grep { $_ & 1 } (1 .. 10) to get a list of odd numbers between 1 and 10.

Thus, you can do any filtering you want with grep, with the proper conditional expression. What you're looking for could be written something like this (with some regexes I made up):

my $url = 'http://www.perlmonks.org/'; my @regexes = ('/private(?:/|$)', 'python'); if (grep { $url =~ $_ } @regexes) { # handle bad URL } else { # handle good URL }
As suggested in another response, you'd probably want to precompile the regexes with qr// for efficiency.

In reply to Re: Reverse grep? by chipmunk
in thread Reverse grep? by JamesA

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found