in reply to Efficient array element deletion

If you aren't afraid of a few counters you could do something like this:
my $replace = 0; for ( my $x = 0; $x< @array; $x++ ) { if ( # pass of your condition ) { $array[$replace] = $array[$x]; $replace++; } }
This will effectively 'shift' every thing over to the front of your array, avoiding double memory issues, and all you have to do is one final pass to "cleanup" the invalid entries at the end (pop @array until the length == $replace).

Replies are listed 'Best First'.
Re^2: Efficient array element deletion
by GrandFather (Saint) on Dec 04, 2008 at 23:36 UTC

    And if you aren't afraid of Perl you can:

    my $replace = 0; for my $x (0 .. $#array) { next unless # pass of your condition; $array[$replace++] = $array[$x]; }

    or maybe even:

    your condition and $array[$replace++] = $array[$_] for 0 .. $#array;

    Perl's payment curve coincides with its learning curve.