in reply to Print both elements in a compare

I think there is a module Array::Compare which will do what you want. If not, adapt following to suit you needs. Shows a couple of methods, more variations are possible as this can get complicated. Most simple is version2 when arrays are of the same size and sort order (don't have to worry about some undefined value). The below will not "blow up" if array's are of different size, but result could wind up being hard to interpret.
#!/usr/bin/perl -w use strict; my @a = qw(a b c e); my @b = qw(a b c d g); compare1 (\@a,\@b); compare2 (\@a,\@b); sub compare1 { my ($aref, $bref) = @_; my %a = map{$_=>0}@$aref; my %b = map{$_=>0}@$bref; foreach (keys %a) { $a{$_}++ if (exists ($b{$_})); } foreach (keys %b) { $b{$_}++ if (exists $a{$_}); } foreach (keys %a) { print "$_ does not exist in B\n" if !$a{$_}; } foreach (keys %b) { print "$_ does not exist in A\n" if !$b{$_}; } } sub compare2 { my ($aref, $bref) = @_; my @a = @$aref; my @b = @$bref; while (@a || @b) { my ($compa, $compb) = (shift @a, shift @b); $compa = 'undefined' if (!defined($compa)); $compb = 'undefined' if (!defined($compb)); if ($compa ne $compb) { print "$compa and $compb do not match\n"; } } } __END__ e does not exist in B g does not exist in A d does not exist in A e and d do not match undefined and g do not match

Replies are listed 'Best First'.
Re^2: Print both elements in a compare
by Anonymous Monk on Nov 02, 2010 at 08:26 UTC
    Thanks for the information. I wanted to avoid using modules becuase I don't learn anything by doing it that way.
      Perl is a "get the job done" kind of S/W tool and many of the Perl modules do help. They aren't all good or wise to use in all situations. However, when they do some "grunt work" for you so that you can think about bigger problems, I figure that is just fine. But when 5 lines of code does all you need and exactly what you need, then that's a different story.