If you want to do something special with your arrays you could write your own function.

This one I wrote a while ago to solve a special problem, perhaps it is of use for you or give other hints:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub array_differenz ($$); main(); exit; sub main { my @a1 = qw/1 2 2 3 3 3 4 4 4 4/; my @a2 = qw/1 2 3 4/; my @a = array_differenz(\@a1, \@a2); print "Result a:\n", Dumper \@a; print "-"x40, "\n"; my @b1 = qw/one one two/; my @b2 = qw/one two/; my @b = array_differenz(\@b1, \@b2); print "Result b:\n", Dumper \@b; print "-"x40, "\n"; my @c1 = qw/one one two/; my @c2 = qw/one two three/; my @c = array_differenz(\@c1, \@c2); print "Result c:\n", Dumper \@c; } # sub main sub array_differenz ($$) { my $a1 = shift; # array reference my $a2 = shift; # array reference my @a1m2 = @$a1; # array 1 minus array 2; for my $element (@$a2) { for my $index (0..$#a1m2) { if ($element eq $a1m2[$index]) { splice @a1m2, $index, 1; last; } } } my @a2m1 = @$a2; # array 2 minus array 1; for my $element (@$a1) { for my $index (0..$#a2m1) { if ($element eq $a2m1[$index]) { splice @a2m1, $index, 1; last; } } } print "Array 1:\n", Dumper $a1; print "Array 2:\n", Dumper $a2; print "Array 1 minus Array 2:\n", Dumper \@a1m2; print "Array 2 minus Array 1:\n", Dumper \@a2m1; return (@a1m2, @a2m1); } # sub array_differenz
it prints:
Array 1: $VAR1 = [ '1', '2', '2', '3', '3', '3', '4', '4', '4', '4' ]; Array 2: $VAR1 = [ '1', '2', '3', '4' ]; Array 1 minus Array 2: $VAR1 = [ '2', '3', '3', '4', '4', '4' ]; Array 2 minus Array 1: $VAR1 = []; Result a: $VAR1 = [ '2', '3', '3', '4', '4', '4' ]; ---------------------------------------- Array 1: $VAR1 = [ 'one', 'one', 'two' ]; Array 2: $VAR1 = [ 'one', 'two' ]; Array 1 minus Array 2: $VAR1 = [ 'one' ]; Array 2 minus Array 1: $VAR1 = []; Result b: $VAR1 = [ 'one' ]; ---------------------------------------- Array 1: $VAR1 = [ 'one', 'one', 'two' ]; Array 2: $VAR1 = [ 'one', 'two', 'three' ]; Array 1 minus Array 2: $VAR1 = [ 'one' ]; Array 2 minus Array 1: $VAR1 = [ 'three' ]; Result c: $VAR1 = [ 'one', 'three' ];
HTH

In reply to Re: How do I compare two arrays? by Crian
in thread How do I compare two arrays? by luoina

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.