in reply to Re: Remove array elements dynamically! (reverse)
in thread Remove array elements dynamically!

assuming of course that the indexes are sorted in ascending order. Safer is:

foreach my $i (sort {$b <=> $a} @arrayindex) { splice @array, $i, 1; }

which sorts in descending order thus guaranteeing the indexes are sorted, and removing the need for the reverse.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^3: Remove array elements dynamically! (reverse)
by lodin (Hermit) on Aug 27, 2007 at 23:11 UTC

    Assuming of course that there are no duplicate indexes. Super-safest is:

    foreach my $i (sort { $b <=> $a } unique(@arrayindex)) { splice @array, $i, 1; }

    ;-)

    lodin

      or:

      my %unique; @unique{@arrayindex} = (); foreach my $i (sort { $b <=> $a } keys %unique) { splice @array, $i, 1; }

      DWIM is Perl's answer to Gödel