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';

In reply to Union, Intersection, and Difference between Arrays by fongsaiyuk

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.