In most cases, splice is the simplest way of removing an item from the middle of an array but it is a dodgey practice to modify an array that you are iterating over using a for/foreach loop. Changing the array whilst you are halfway through it is not a good idea.

The easiest way to do what you want is to set the elements that are to be removed to undef in the loop and then use grep to remove them at the end.

#! perl -slw use strict; my @array = (1 .. 100); print scalar @array; for my $i (0 .. $#array) { $array[$i] = undef if $array[$i] % 10 == 0; } print scalar @array; @array = grep{ $_ } @array; print scalar @array;

It is safe to remove elements from the top of the array whilst if you process the array in reverse order.

#! perl -slw use strict; my @array = (1 .. 100); print scalar @array; my @toDelete; for my $i (reverse 0 .. $#array) { splice(@array, $i, 1) if $array[$i] % 10 == 0; } print scalar @array;

You can also accumulate an array of indexes for the elements to be removed and use delete on an array slice built from the index array. Unfortunately, delete will only remove the element from the array completely of it happens to be the last element. Otherwise it just sets it to be undef and you still need to use grep to trim the rest out, so there is no advantage with this over the first method above.


Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.


In reply to Re: Removing Items from an Array by BrowserUk
in thread Removing Items from an Array by ibanix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.