Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How do I completely remove an element from an array?

( #1871=categorized question: print w/ replies, xml ) Need Help??
Contributed by vroom on Jan 08, 2000 at 08:20 UTC
Q&A  > arrays


Answer: How do I completely remove an element from an array?
contributed by vroom

splice was designed to handle this.

splice @array, $indextoremove, 1;

splice can be called in any of the following forms:

splice ARRAY, OFFSET, LENGTH #removes anything from the OFFSET index through the index OFFSET+LENGT +H-1 splice ARRAY, OFFSET, LENGTH, LIST #same as the previous but replaces elements removed with those in LIST splice ARRAY, OFFSET #removes anything from the OFFSET index on

In any of these cases, splice returns the elements it removed.

Answer: How do I completely remove an element from an array?
contributed by jacques

I like using grep for this.

@array = grep { $_ != $element_omitted } @array;
(Be sure to use a test appropriate to the values in your array!)
Answer: How do I completely remove an element from an array?
contributed by jaa

Be careful using splice during array iteration.

Note the subtle bug here:

my @items = ( 0..5 ); my $index = 0; for my $value (@items) { print "testing $value\n"; if ( $value == 1 or $value == 3 ) { print "removed value $value\n"; splice @items, $index, 1; } $index++; }

Values 2 and 4 are never actually tested for; iteration skips them completely!

If you want to iterate over an array removing some items, try something like:

my $index = 0; while ($index <= $#items ) { my $value = $items[$index]; print "testing $value\n"; if ( $value == 1 or $value == 3 ) { print "removed value $value\n"; splice @items, $index, 1; } else { $index++; } }

And, this being Perl I bet there are many other safe iteration/splice approaches... 8-)

Answer: How do I completely remove an element from an array?
contributed by screamingeagle

how about :

undef $array[$i]
where $i is the element u wish to remove...

This will not actually remove an element from the array, but will set the value to undef, which will mean that $array[$i] is not defined. scalar @array will still return the same number as before. Here's an example:

my @array = qw( 1 2 3 4 ); print "there are ".scalar(@array)." elements in \@array \n"; print "the last index of \@array is $#array \n"; print "the 3'rd (index 2) value of \@array is $array[2] \n"; undef $array[2]; print "\$array[2] is now undefined \n" if not defined $array[2]; print "there are STILL ".scalar(@array)." elements in \@array \n"; print "the last index of \@array is STILL $#array \n";
Answer: How do I completely remove an element from an array?
contributed by OzVegan

I'd iterate backwards through the array therefore avoiding the missing element syndrome when using an index counter. So if we were removing 1 and 3 from the following array...

my @array = qw( alpha beta gamma delta ); for ( my $index = $#array; $index >= 0; --$index ) { splice @array, $index, 1 if $array[$index] =~ /e/; # remove certain elements }
Answer: How do I completely remove an element from an array?
contributed by Monkomatic

You can use delete, as in:

delete $ARRAY[$index];
but note the warning:
Calling delete on array values is deprecated and likely to be removed in a future version of Perl.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (5)
    As of 2014-07-29 05:48 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (211 votes), past polls