Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: grep { $var } @arr

by Laurent_R (Canon)
on Jul 17, 2014 at 18:55 UTC ( [id://1094110]=note: print w/replies, xml ) Need Help??


in reply to Re: grep { $var } @arr
in thread grep { $var } @arr

Well, bulrush, you did not say how you populate $pat, but I fail to see how a regex would figure out whether a value is larger or smaller than 3.

I definitely agree that a subroutine reference is a very good solution, perhaps the best.

Replies are listed 'Best First'.
Re^3: grep { $var } @arr
by AppleFritter (Vicar) on Jul 21, 2014 at 09:54 UTC

    I fail to see how a regex would figure out whether a value is larger or smaller than 3.

    You can get clever with match-time pattern interpolation. For instance:

    my @list = (1..5); my $pattern = qr/\d+(??{if($& < 3) { "" } else { "(?<!\\d)" } })/; my @res = grep /$pattern/, @list; say join ", ", @res;

    This also (ab)uses negative look-behind assertions to create a subpattern that is guaranteed to fail (in this regex, not in general).

    But I agree, subroutine references are the way to go here.

      Yeah, that's clever ++, that that expression is far from being regular.

Log In?
Username:
Password:

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

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

    No recent polls found