Try this:

my @a = ( 1, 2, 3, 4, 5, 6 ); my @b = ( 3, 4, 5, 6, 7, 8 ); my %a_lookup; @a_lookup{@a} = (); my @b_only = grep { not exists $a_lookup{$_} } @b; print "@b_only\n";

If my calculations are correct, this ought to do the trick. ;)

Update: Whenever I see a question like this I think to myself, "That sounds like something Quantum::Superpositions could do with grace and ease. While it's true that the Damian's module does accomplish this task simply and easily, it took a conversation in the Chatterbox with tye and merlyn before the actual solution became apparent to me. Credit merlyn for coming up with the Quantum::Superpositions solution at first guess... for my part, I just started the conversation rolling until an expert could come along and get it right. ;)

use Quantum::Superpositions; my @a = ( 1 .. 6 ); my @b = ( 3 .. 8 ); print "$_\n" foreach eigenstates( any( @b ) != all( @a ) );

Beauty is in the eye of the beholder, but to my mind's eye that's beautiful. Unfortunately, its simplicity is critically deceptive; don't try this with lists of a thousand elements. Using quantum superpositions on large sets becomes breathtakingly inefficient.


Dave


In reply to Re: array difference by davido
in thread array difference 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.