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.)