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


In reply to Re: Fastest way to grep multiple time on some array by gone2015
in thread Fastest way to grep multiple time on some array by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.