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.