Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

array against array regexp
Hi all,

let's we have:

use strict;
use warnings;

my @input = qw{ one two twentythree four };
my @filter = qw { one three five ten twenty };


my @filtered = ();
for my $in (@input) {
for my $filter (@filter) {
push @filtered, $in if $in =~ /$filter$/i;
}
}

I'd like to get new @filtered array, where will be only values
from @input against @filter.

but my snippet is 6 lines, I think it should be written in 1 line, with
using grep and map, but I'm not sure how.

Also we can say, input is always smaller than @filter (for
optimalization)

Anyone ?  :)




Report this thread to moderator Post Follow-up to this message
Old Post
Ing. Branislav Gerzo
06-05-05 08:56 PM


Re: array against array regexp
>>>>> "Ing" == Ing Branislav Gerzo <konfera@2ge.us> 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
<merlyn@stonehenge.com> <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
!

Report this thread to moderator Post Follow-up to this message
Old Post
Randal L. Schwartz
06-05-05 08:56 PM


Re: array against array regexp
Randal L. Schwartz [RLS], on ,  , 2005 at 05:47 (-0700) wrote about :

RLS>       $wanted ||= $input =~ /$_/ for @filter;
RLS>       $wanted;
RLS>     } @input;

RLS> Note the use of ||= for short-circuiting the tests once a good filter
RLS> is found.  You can't use "return 1", because a grep/map block is not a
RLS> subroutine.

very nice example, thanks a lot.

--

How do you protect mail on web? I use http://www.2pu.net

["Just give me the original history, please!" -- Sam Beckett]



Report this thread to moderator Post Follow-up to this message
Old Post
Ing. Branislav Gerzo
06-06-05 01:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:45 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.