Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: grep { $var } @arr

by Athanasius (Archbishop)
on Jul 17, 2014 at 14:56 UTC ( [id://1094069]=note: print w/replies, xml ) Need Help??


in reply to grep { $var } @arr

Another option is to use a subroutine reference instead of a string:

#! perl use strict; use warnings; my @arr = (1, 2, 3, 4, 5); my $pat = sub { $_[0] < 3 }; my @res; @res = grep {$_ < 3} @arr; print "Literal:\t@res\n"; @res = grep { $pat->($_) } @arr; print "Variable:\t@res\n";

Output:

0:52 >perl 939_SoPW.pl Literal: 1 2 Variable: 1 2 0:52 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

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

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

    No recent polls found