in reply to Removing array elements

You want the splice operator.

Incidentally, to get away with removing elements from an array in a for loop, you need to take them in reverse order:

for (reverse 0 .. $#a) { splice @a, $_, 1 if condition($a[$_]); }
Many of us would write that with grep: @a = grep { ! condition($_) } @a;

You appear to be taking the set differences of two arrays. That has a well-known technique in perl:

my (%a, %b); @a{@a} = (); @b{@b} = (); delete @a{@b}; delete @b{@a}; @a = keys %a; @b = keys %b; # or if order matters, # @a = grep {exists $a{$_}} @a; # @b = grep {exists $b{$_}} @b;
The funny-looking @a{@a} constructions are hash slices.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Removing array elements
by mrpeabody (Friar) on Aug 25, 2005 at 04:43 UTC
    While splice is the way to remove elements from an array, nb perlsyn:

    If any part of LIST is an array, "foreach" will get very confused if you add or remove elements within the loop body, for example with "splice". So don't do that.

    In this situation I usually (as others have suggested) use a C-style for loop.

      Hence reverse. If you remove or insert elements from the end, the positions of unvisited elements are not affected.

      After Compline,
      Zaxo