#!/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_whether_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";