If you need to sort them anyways, an easy way to do it is first sort the array. With the array sorted, identical items will be right next to each other, so to eliminate duplicates you only have to check if an item is the same as the one immediately preceding it, and if so delete it (or not add it to the results). That's essentially how the Unix utility uniq(1) works.

Here's some code I use to do this. It takes a comparison coderef as the first parameter, much like sort, and the array to be sorted and uniqified as the second.

sub uniq(&@) { my($cmpsub, @list)=@_; my $last = shift @list or return (); my @ret =($last); foreach (@list) { push(@ret,$_) unless sortcmp($cmpsub,$_,$last)==0; $last = $_; } @ret; }

My suspicion is that doing this will be slightly faster than using a hash for very large arrays, but I haven't measured it.


In reply to Re: slurping into a substring'ed' array by sgifford
in thread slurping into a substring'ed' array by seaver

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.