This kind of problem cries out (in my mind, at least) for the use of a hash (associative array). I generally load up a common hash with the values of one list, and then step through the second list doing the same. The resulting hash will have one entry for each unique key, since hashes resolve collisions in that way.

Similar problems like finding the intersection between two sets, or getting the complement of the intersection, can be handled in like manner, by incrementing the value of the hash or deleting hash entries on a match.

Of course, there's more than one way to do it. :)

Update: Ah, I see [id://Tanktalus] has already hinted in this direction. Darn those 'Johnny-on-the-spot' Monks!

See also the timely Re: How can I assign the elements in an array to only the key values in a hash?, which bears on your problem.


No good deed goes unpunished. -- (attributed to) Oscar Wilde

In reply to Re: removing doubles by comparing two strings - newbie question! by ptum
in thread removing doubles by comparing two strings - newbie question! by Anonymous Monk

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.