### How do I compare two arrays?

by luoina (Acolyte)
 on Mar 09, 2004 at 06:41 UTC Need Help??
luoina has asked for the wisdom of the Perl Monks concerning the following question:

Dear All: Suppose I have two arrays, @a1 and @a2. I want to compare these two arrays. Any elements contained in @a1 will be deleted from @a2. How do I do that? What will the code look like? Thank you very much in advance. ......luoina

Replies are listed 'Best First'.
Re: How do I compare two arrays?
by AcidHawk (Vicar) on Mar 09, 2004 at 06:44 UTC
Thank you so much. It helps a lot.....luoina
Re: How do I compare two arrays?
by Caron (Friar) on Mar 09, 2004 at 08:07 UTC

Two solutions:

1. Using Super Search:

and more

2. Using the FAQ (or perldoc -q arrays)

HTH

Re: How do I compare two arrays?
by Crian (Chaplain) on Mar 09, 2004 at 10:29 UTC
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
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";

Re: How do I compare two arrays?
by DrHyde (Prior) on Mar 09, 2004 at 08:43 UTC
This is the most efficient possible implementation, as given appropriate hardware it returns in constant time even for really large arrays. All other solutions are at least O(n).
```use Quantum::Superpositions;
@a2 = eigenstates(any(@a2) != all(@a1));
How this works is left as an exercise for the reader, as you wouldn't want to submit this answer for your homework without explaining how it works.
This is the most efficient possible implementation, as given appropriate hardware it returns in constant time even for really large arrays. All other solutions are at least O(n).
Eh, no, actually. With Θ (n2) processors, you can solve this in Ο (log n) time. And that's with off-the-self processors, not some hardware which is unlikely to be ever build.

Abigail

Re: How do I compare two arrays?
by matija (Priest) on Mar 09, 2004 at 07:41 UTC
You've already got a pointer to the QA section.

Another solution (that I didn't see mentioned there) is to transfer elements of one array into a hash, and use that.

Such a method uses more memory space but is faster (unless you use so much memory you start swapping :-)

The code would look somewhat like this:

```map(\$a1{\$_}=1,@a1); # create %a with elements from @a1
@a2=grep(!defined(\$a1{\$_}),@a2); # returns those elements of @a2 that
+don't
# have a hash element in %a1 therefore they
# do not exist in @a1.

You should be more careful with your assertions:

```@a1 = qw(1 2 2 3);
@a2 = qw(1 2 3 3);

Your code will consider @a1 and @a2 as equal when they clearly aren't. Your solution can only work when all array elements are unique and the order of elements is irrelevant. The original poster possibly wants an unordered comparision, but the problem with multiple identical elements remains.

If you modify your code to use the hash as a counter, then it works better:

```use strict;

sub dec(\$) {
\$_[0] ? \$_[0]--
: 0
};

my @a1 = qw(1 2 2 3);
my @a2 = qw(1 2 3 3);

my %a1;

\$a1{\$_}++
for (@a1);

my @only_a2 = map { dec \$a1{\$_} ? () : \$_ } @a2;
print "Only in \@a2 : @only_a2\n";

In the end, I guess what the poster really wants is what's called the symmetric difference between two sets, which is what you (for example) get by building @only_a1 and @only_a2.

Re: How do I compare two arrays?
by BUU (Prior) on Mar 09, 2004 at 06:43 UTC
How do I do that?
Probably using perl.
What will the code look like?
My guess is a series of perl statements.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://335021]
Approved by kvale
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2017-08-18 01:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (294 votes). Check out past polls.

Notices?