>>>>> "Ing" == Ing Branislav Gerzo <[EMAIL PROTECTED]> writes:
Ing> Hi all,
Ing> let's we have:
Ing> use strict;
Ing> use warnings;
Ing> my @input = qw{ one two twentythree four };
Ing> my @filter = qw { one three five ten twenty };
Ing> my @filtered = ();
Ing> for my $in (@input) {
Ing> for my $filter (@filter) {
Ing> push @filtered, $in if $in =~ /$filter$/i;
Ing> }
Ing> }
Ing> I'd like to get new @filtered array, where will be only values
Ing> from @input against @filter.
Ing> but my snippet is 6 lines, I think it should be written in 1 line, with
Ing> using grep and map, but I'm not sure how.
## first, precompile all the regex for speed:
@filter = map qr/$_$/i, @filter;
## now grep the wanteds
my @filtered = grep {
my $wanted = 0;
my $input = $_;
$wanted ||= $input =~ /$_/ for @filter;
$wanted;
} @input;
Note the use of ||= for short-circuiting the tests once a good filter
is found. You can't use "return 1", because a grep/map block is not a
subroutine.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>