rsiedl has asked for the wisdom of the Perl Monks concerning the following question:
This returns:#!/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;
----------------------
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.but would still be interested in how you would go about it...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);
Update 2: This works but seems long winded...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);
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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Compare Lists (Non Unique Intersection)
by davido (Cardinal) on Jul 28, 2006 at 06:23 UTC | |
|
Re: Compare Lists (Non Unique Intersection)
by Sidhekin (Priest) on Jul 28, 2006 at 06:24 UTC | |
by rsiedl (Friar) on Jul 28, 2006 at 06:45 UTC | |
|
Re: Compare Lists (Non Unique Intersection)
by crashtest (Curate) on Jul 28, 2006 at 06:23 UTC | |
|
Re: Compare Lists (Non Unique Intersection)
by GrandFather (Saint) on Jul 28, 2006 at 06:42 UTC | |
|
Re: Compare Lists (Non Unique Intersection)
by Mandrake (Chaplain) on Jul 28, 2006 at 08:42 UTC | |
|
Re: Compare Lists (Non Unique Intersection)
by Mandrake (Chaplain) on Jul 28, 2006 at 06:43 UTC | |
|
Re: Compare Lists (Non Unique Intersection)
by jwkrahn (Abbot) on Jul 28, 2006 at 10:53 UTC |