Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How do I compare two arrays?

by Crian (Curate)
on Mar 09, 2004 at 10:29 UTC ( [id://335049]=note: print w/replies, xml ) Need Help??


in reply to How do I compare two arrays?

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

Replies are listed 'Best First'.
Re^2: How do I compare two arrays?
by bimleshsharma (Beadle) on Jun 30, 2011 at 06:33 UTC
    The best way what i find is to compare

    use FreezeThaw qw(cmpStr);

    @a =(1,2,3,4);
    @b = ( "this", "that", "more", "stuff" );
    @c = (1,2,3,5);

    printf "a and b contain %s arrays\n",cmpStr(\@a, \@b) == 0 ? "the same" : "different";

    printf "a and c contain %s arrays\n",cmpStr(\@a, \@c) == 0 ? "the same" : "different";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://335049]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found