Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

(tye)Re: Deleting an element from the middle of an array

by tye (Sage)
on Jan 08, 2001 at 09:43 UTC ( #50447=note: print w/replies, xml ) Need Help??


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:

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

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2020-01-26 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?