in reply to Remove array elements dynamically!

If your indexes are sorted, you could keep a separate counter for the number of items removed and subtract it from $i when splicing.

However, it's much easier and more efficient to create a new array instead of splicing the same array, since each splice needs to copy all the elements from $i to the end of the array rearrange/copy the pointers to the elements from element $i to the end of the array.

my @newarray; foreach my $i (@arrayindex) { push @newarray,$array[$i]; }
Depending on your algorithm, you could probably do that at the same time/instead of building the @arrayindex array.

Another way would be to use grep (if you can determine which elements to keep based solely on their value):

my @kept = grep { $_ < 4 and $_ > 10 } @array;
or even
@array = grep { $_ < 4 and $_ > 10 } @array;

Replies are listed 'Best First'.
Re^2: Remove array elements dynamically!
by shmem (Chancellor) on Aug 27, 2007 at 19:59 UTC
    ... since each splice needs to copy all the elements from $i to the end of the array.

    Is that so? I always thought of splice being highly efficient, working "in-place", just "re-arranging elements", without need to copy anything. Can you ascertain your statement?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re^2: Remove array elements dynamically!
by suaveant (Parson) on Aug 28, 2007 at 20:21 UTC
    Ummm... you do realize that, while a valid example, nothing can ever be less than 4 AND greater than 10?
    array = grep { $_ > 4 and $_ < 10 } @array;

                    - Ant
                    - Some of my best work - (1 2 3)

Re^2: Remove array elements dynamically!
by newbio (Beadle) on Aug 27, 2007 at 20:05 UTC
    Thank you Joost. I am interested in the array that is left after removing/splicing out +the indices (defined in @arrayindex) from @array. But, I have got an idea from your hint. Let's see. Thanks.