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")In reply to (tye)Re: Deleting an element from the middle of an array
by tye
in thread Deleting an element from the middle of an array
by Intaglio
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |