in reply to Foreach for array pruning?

I think I prefer your grep solution to the syntax you're asking for. Anyhow, the interior of for (@foo) {} doesn't know what position in the array $_ represents. You can do what you want with the indexes if you take them in reverse order to prevent splice from altering the yet-to-be-seen positions.

for (reverse 0 .. $#moo) { splice @moo, $_, 1 if $moo[$_] % 2; }
I imagine that all that splicing is less efficient than grep assignment. Notice that comparison with zero is unnecessary in the conditional.

This is not too different from your C style loop, but corrects its major flaw.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Foreach for array pruning?
by demerphq (Chancellor) on Jul 02, 2004 at 21:46 UTC

    I imagine that all that splicing is less efficient than grep assignment.

    Definately. using splice like this is horribly inefficient in terms of run time. Every splice requires the entire array to be shifted, and each time with more elements. Which if my hungover brain is correct would run in O(N**2): ie horribly slow.


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi