in reply to Deleting an element from the middle of an array

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:

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")

Replies are listed 'Best First'.
Re: (tye)Re: Deleting an element from the middle of an array
by chipmunk (Parson) on Jan 08, 2001 at 09:53 UTC
    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.)