Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

An efficient way to remove an element from an array

by sophate (Beadle)
on Aug 06, 2012 at 10:22 UTC ( #985647=perlquestion: print w/replies, xml ) Need Help??
sophate has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

pop and shift can remove an element from an array at the end or from the beginning. Is there any efficient way to remove an array element in the middle? I'm doing this with the codes below. But just wondering if there is any better way of doing this? ("delete" only sets the element to undef but doesn't remove it from the array)

#!/usr/bin/perl -w use warnings; use strict; my @FullArray=qw(1 2 3 4 5 6); print "The number of elements: [",scalar(@FullArray),"]\n"; my $LastElement = $#FullArray; print "Removing the 3rd element . . .\n"; my @Temp = @FullArray[0..1]; @FullArray = (@Temp, @FullArray[3..$LastElement]); print "The number of elements: [",scalar(@FullArray),"]\n"; print "The remaining elements:[@FullArray]\n";

Replies are listed 'Best First'.
Re: An efficient way to remove an element from an array
by Corion (Pope) on Aug 06, 2012 at 10:28 UTC

    See splice. For understanding splice I really recommend reimplementing the pop, push, shift and unshift functions using only splice.

      The documentation kinda gives away the implementation :) literally :)

        Sure, but working through the why and how is still educative :).

      Thanks. It's exactly what I'm looking for :)

Re: An efficient way to remove an element from an array
by AnomalousMonk (Chancellor) on Aug 06, 2012 at 19:10 UTC

    Here's an 'efficient' method if you're completely unconcerned about maintaining array order. (This was published by someone else recently in response to a strikingly similar question.)

    >perl -wMstrict -le "my @ra = (0, 1, 2, 3, 4, 5); print qq{@ra}; for my $i (2, 0, -2, -1) { $ra[$i] = $ra[-1]; --$#ra; print qq{removed element at index $i: @ra}; } " 0 1 2 3 4 5 removed element at index 2: 0 1 5 3 4 removed element at index 0: 4 1 5 3 removed element at index -2: 4 1 3 removed element at index -1: 4 1
Re: An efficient way to remove an element from an array
by linuxkid (Sexton) on Aug 06, 2012 at 15:50 UTC

    delete $arrray[$index];


      Let me try to explain why this response might have attracted so many downvotes, and more importantly, why it's not good advice.

      Actually, the documentation does a better job than I could do:

      delete() may also be used on arrays and array slices, but its behavior is less straightforward. Although exists() will return false for deleted entries, deleting array elements never changes indices of existing values; use shift() or splice() for that. However, if all deleted elements fall at the end of an array, the array's size shrinks to the position of the highest element that still tests true for exists(), or to 0 if none do.

      WARNING: Calling delete on array values is deprecated and likely to be removed in a future version of Perl.

      (Emphasis added.)


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://985647]
Approved by Ratazong
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2018-05-22 09:10 GMT
Find Nodes?
    Voting Booth?