Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Union, Intersection, and Difference between Arrays

by fongsaiyuk (Pilgrim)
on Mar 16, 2001 at 01:44 UTC ( #64798=snippet: print w/replies, xml ) Need Help??
Description: One of my projects required me to find the difference between two arrays. I look through Super Search popped this node: RE: Comparing arrays.

UPDATED: Er. work project that is, not homework project.

A look through the language.perl.org perlfaq4 showed a snippet of code.

I added some Dumper routines to see what was actually going on and flushed it out for my purposes. I thought I'd include it here as it helped me to better understand the answer in the FAQ but basically it's the code snippet from the FAQ implememented as something that made sense to me. ^-^ Maybe it would help out someone else?

fong

#!/usr/bin/perl -w
#
# Union, Intersection, and Difference between two arrays
#
# 03.15.2001
# A quick little hack based on information contained in 
# this node:
# http://perlmonks.org/index.pl?node_id=5733
#
# As an example of testing two array's equality
#
# This is taken directly out of the faq here:
# http://language.perl.com/newdocs/pod/perlfaq4.html#How_do_I_test_whe
+ther_two_arrays
#
#
#
use strict;
use Data::Dumper;
 
my @a1 = ("CUSTORDS.DAT",
        "RECEIPTS.DAT");
 
print "Array 1:\n";
print Dumper(@a1),"\n";

my @b1 = ("CUSTORDS.DAT",
        "ONHAND.DAT",
        "RECEIPTS.DAT");
print "Array 2:\n";
print Dumper(@b1),"\n";

my @union = my @inter = my @diff = ();
my %count = ();

foreach my $element (@a1, @b1) {
        $count{$element}++;
};
foreach my $element (keys %count) {
        push @union, $element;
        push @{ $count{$element} > 1 ? \@inter : \@diff }, $element;
};

print "Count:\n";
print Dumper(%count),"\n";
print "Union:\n";
print Dumper(@union),"\n";
print "Intersection:\n";
print Dumper(@inter),"\n";
print "Difference:\n";
print Dumper(@diff),"\n";


Sample Output:
Array 1:
$VAR1 = 'CUSTORDS.DAT';
$VAR2 = 'RECEIPTS.DAT';

Array 2:
$VAR1 = 'CUSTORDS.DAT';
$VAR2 = 'ONHAND.DAT';
$VAR3 = 'RECEIPTS.DAT';

Count:
$VAR1 = 'ONHAND.DAT';
$VAR2 = '1';
$VAR3 = 'RECEIPTS.DAT';
$VAR4 = '2';
$VAR5 = 'CUSTORDS.DAT';
$VAR6 = '2';

Union:
$VAR1 = 'ONHAND.DAT';
$VAR2 = 'RECEIPTS.DAT';
$VAR3 = 'CUSTORDS.DAT';

Intersection:
$VAR1 = 'RECEIPTS.DAT';
$VAR2 = 'CUSTORDS.DAT';
 
Difference:
$VAR1 = 'ONHAND.DAT';

Replies are listed 'Best First'.
Re (tilly) 1: Union, Intersection, and Difference between Arrays
by tilly (Archbishop) on Mar 17, 2001 at 22:00 UTC
    If you are going to play, it is worthwhile trying to extract the functionality into functions. Then if it is useful to someone they can just call your functions.

    Also if one element has duplicate elements but the other does not, then the element will show up in your intersection which it should not. Which underscores the value of writing functions in the first place. If you cut and paste the above, then discovered the bug, you would likely have repeated the bug all over. By contrast if you wrote functions then you would have limited the effort of fixing the bug just to the key functions where it appeared...

      Thank you very much !
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://64798]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2019-08-25 22:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?