Hi Monks,

I'm trying to compare two arrays and find their "intersection". List::Compare has so far proved very useful, however I now need a non-unique intersection.
Example code:
#!/usr/bin/perl use strict; use List::Compare; my @temp = ( 'test', 'test1', 'test', 'test2', 'test2', 'test' ); print "----------------------\nContents of Temp Array\n"; print "\t", $_, "\n" foreach (@temp); my @temp2 = ( 'test1', 'test2', 'test2' ); print "----------------------\nContents of Temp2 Array\n"; print "\t", $_, "\n" foreach (@temp2); my $lc = List::Compare->new('--unsorted', \@temp, \@temp2); my @intersection = $lc->get_intersection; print "----------------------\nThe (Unique) Intersection of Temp and T +emp2 Arrays\n"; print "\t", $_, "\n" foreach (@intersection); exit;
This returns:
----------------------
Contents of Temp Array
        test
        test1
        test
        test2
        test2
        test
----------------------
Contents of Temp2 Array
        test1
        test2
        test2
----------------------
The (Unique) Intersection of Temp and Temp2 Arrays
        test1
        test2
However I need the Intersection to look like:
The Intersection of Temp and Temp2 Arrays
        test1
        test2
        test2
as test2 appeared twice in both lists.

I cant find a way to do this with List::Compare, so can anyone suggest anything?

Cheers,
Reagen

Update: I've figured out a way to do it As pointed out by Sidhekin, this doesnt work.
my %temp=map{$_ =>1} @temp; my %temp2=map{$_=>1} @temp2; my @non_unique_intersection = grep( $temp{$_}, @temp2 ); print "----------------------\nThe Non-Unique Intersection of Temp and + Temp2 Arrays\n"; print "\t", $_, "\n" foreach (@non_unique_intersection);
but would still be interested in how you would go about it...

For bonus points can anyone suggest a way to get the following:
- The non-unique leftover from @temp; and,
- The non-unique leftover from @temp2.

I tried the following which doesnt work...
my @non_unique_Lonly = @temp; for (my $i=0;$i<scalar(@non_unique_Lonly);$i++){ delete $non_unique_Lonly[$i] if ( grep $_ eq $non_unique_Lonly[$i], +@non_unique_intersection ); } # end-for print "----------------------\nThe Non-Unique Leftover from Temp\n"; print "\t", $_, "\n" foreach (@non_unique_Lonly);
Update 2: This works but seems long winded...
my @non_unique_Lonly = @temp; my @temp_intersection = @non_unique_intersection; for (my $i=0;$i<scalar(@non_unique_Lonly);$i++){ for (my $j=0;$j<scalar(@temp_intersection);$j++){ if ( grep $_ eq $non_unique_Lonly[$i], @temp_intersection ) { delete $non_unique_Lonly[$i]; delete $temp_intersection[$j]; last; } # end-if } # end-for } # end-for print "----------------------\nThe Non-Unique Leftover from Temp\n"; print "\t", $_, "\n" foreach (@non_unique_Lonly);

In reply to Compare Lists (Non Unique Intersection) by rsiedl

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.