in reply to Remove array elements dynamically!

Just do it from the back.

foreach my $i (reverse @arrayindex) { splice @array, $i, 1; }

lodin

Update: This assumes sorted indexes. See GrandFather's note if they're not sorted.

Replies are listed 'Best First'.
Re^2: Remove array elements dynamically! (reverse)
by GrandFather (Saint) on Aug 27, 2007 at 21:02 UTC

    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

      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