Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Fastest way to grep multiple time on some array

by gone2015 (Deacon)
on Feb 19, 2009 at 17:15 UTC ( [id://745128] : note . print w/replies, xml ) Need Help??


in reply to Fastest way to grep multiple time on some array

Can you grep and remove simultaneously?

I don't think so... I tried:

my @A = grep m/A/ && !defined($_ = undef), @foo ;
and ended up with @A containing 'n' undef values...

The following at least works:

use strict ; use warnings ; my @foo = ('A', 'B', 'C', 'A', 'B', 'A', 'D') ; my $q ; my @A = map defined($_) && m/A/ ? scalar($q = $_, $_ = undef, $q) : +(), @foo ; my @B = map defined($_) && m/B/ ? scalar($q = $_, $_ = undef, $q) : +(), @foo ; my @C = map defined($_) && m/C/ ? scalar($q = $_, $_ = undef, $q) : +(), @foo ; @foo = grep defined($_), @foo ; print "A: @A\n" ; print "B: @B\n" ; print "C: @C\n" ; print " @foo\n" ;
BUT I'm making no claim for it efficiency-wise. (Also, it's ugly as sin.)