Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-23 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found