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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.