Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Deleting an element from the middle of an array

by Intaglio (Novice)
on Jan 08, 2001 at 02:23 UTC ( #50396=snippet: print w/replies, xml ) Need Help??
Description: This is used when you need to delete an element of an array that can't be done easily with shift or pop, i.e. one that's in the middle. In this example @array is the array in question.
$val = $array[$ELEMENT_NUMBER_WE_WANT_DELETED];
%new_array = map { 
                 $_ => "1"
                 } @array;
delete $new_array{$val};
@array = keys %new_array;
Replies are listed 'Best First'.
Re: Deleting an element from the middle of an array
by salvadors (Pilgrim) on Jan 08, 2001 at 02:27 UTC

    Or alternatively, use splice, which will be much quicker, and also not break arrays that happen to have more than one entry with the same value ...

    splice @array, $ELEMENT_NUMBER_WE_WANT_DELETED, 1;

    Tony

(Ovid) Re: Deleting an element from the middle of an array
by Ovid (Cardinal) on Jan 08, 2001 at 02:30 UTC
    In addition to the comment by salvadors, I'd like to point out that splice also allows you to keep the array order, whereas your solution does not.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

(tye)Re: Deleting an element from the middle of an array
by tye (Sage) on Jan 08, 2001 at 09:43 UTC

    Others have mentioned that your method has a problem in that it doesn't preserve order. This is a serious problem when dealing with arrays, but your solution has a few other serious problems as well:

    • Duplicate values are removed
    • All values are forced to be strings
    • It is a ton slower than splice for large arrays

    Of those, the middle one could possibly use some explanation. For example, if the array contains undef, then when you are done the array will instead contain "". If the array contains a bunch of objects, then when you are done the array will contain a bunch of strings like "Some::Module=HASH(0x1ec6218)" that will be unusable as objects.

    Wouldn't it be neat if delete $array[$n] would be interpretted as splice( @array, $n, 1 )? This would be even neater when deleting several elements because delete @array[@indices] could be optimized to be much faster than could be written directly in Perl.

    Oh well, that would a bit of work (and would the opimization be faster if we built a hash from the indices or if we sorted them?). A smaller improvement would be if delete $array[$n] produced a fatal error that mentioned splice. (:

    It looks like delete was changed to act like undef when used on an array element (in Perl 5.6). I would guess this is just a bug. :(

    Update: Sorry, salvadors, I didn't notice your mention of duplicate value elimination at first.

            - tye (but my friends call me "Tye")
      It looks like delete was changed to act like undef when used on an array element (in Perl 5.6). I would guess this is just a bug. :(

      delete() on arrays was actually added as support for pseudo-hashes. It's not a bug, it's just poor design. ;)

      (That last bit is opinion, of course.)

Re: Deleting an element from the middle of an array
by ichimunki (Priest) on Jan 08, 2001 at 04:40 UTC
    Other comments notwithstanding perlfunc:splice also does a whole bunch of other really cool stuff besides ripping elements out of the middle of the list.
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://50396]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2020-01-29 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?