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 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?


#!/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:
# As an example of testing two array's equality
# This is taken directly out of the faq here:
use strict;
use Data::Dumper;
my @a1 = ("CUSTORDS.DAT",
print "Array 1:\n";
print Dumper(@a1),"\n";

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

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

foreach my $element (@a1, @b1) {
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:

Array 2:

$VAR2 = '1';
$VAR4 = '2';
$VAR6 = '2';