I'm not quite sure what you mean by "misses the point"?

I thought you were looking for a way to avoid your percieved inefficiencies of using grep to filter large arrys. The only 'inefficiency' that really exists is that (notionally) a list is created on one side of the grep which is then filtered and assigned back to the array on the other. Ie. Perl trades space for time.

Given the way Perl implements it's arrays, there is no trivial way of removing an element from the middle.

[@myarray]-->[ 0 ]->[ value 1 ] [ 1 ]->[ value 2 ] [ 2 ]->[ value 3 ] [ 3 ]->[ value 4 ] [ 4 ]->[ value 5 ]

To remove a value from the middle (say element 2) in-place, you have to ripple the pointers.

[@myarray]-->[ 0 ]---->[ value 1 ] [ 1 ]---->[ value 2 ] [ 2 ]-\ [ 3 ]-\\->[ value 4 ] \->[ value 5 ]

In effect (if not in exact implementation), is to copy pointers from the old list into a new one omiting those that fail the test. Then discard the old array if the destination is the same as the source. Very efficient cpu-wise but does require a extra memory.

Doing it in-place avoids the extra memory, but there is no way to void the ripple. That's basically what my snippet did. It's obviously not going to be as fast, though I was surprised how little slower it was. Whether it would be possible to add an optimisation to grep to do this in line if the dest == src I'm not sure. I know that sort has a similar 'in-place' optimisation for dest == src invocations.

With respect to the need for the ';'... It's required to allow the parser to determine that your inner code block

@moo = grep {; {if ($_ % 2 > 0){ 0;last; } 1;}} @moo;

Is a code block and not an anonymous hash. More typically this is done with a unary '+'

@moo = grep { +{if ($_ % 2 > 0){ 0;last; } 1;} } @moo;

Alternatively, you could avoid the need for the inner block completely and do

@moo = grep { not ( $_ % 2 ) } @moo;

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

In reply to Re^3: Foreach for array pruning? (Updated) by BrowserUk
in thread Foreach for array pruning? by spandox

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.