in reply to Re^2: grep { $var } @arr
in thread grep { $var } @arr
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: grep { $var } @arr
by Laurent_R (Canon) on Jul 21, 2014 at 17:37 UTC |