# Compare arrays in perl

**URL:** <https://community.unix.com/t/compare-arrays-in-perl/222929>\
**Category:** Shell Programming and Scripting\
**Created:** [December 3, 2008, 10:29am UTC](https://community.unix.com/t/compare-arrays-in-perl/222929 "2008-12-03T10:29:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chriss\_58](https://community.unix.com/letter_avatar/chriss_58/32/6_22734026db12803ebb3e059622ac3534.png) [@chriss\_58](https://community.unix.com/u/chriss_58)\
**Post date:** [December 3, 2008, 10:29am UTC](https://community.unix.com/t/compare-arrays-in-perl/222929/1 "2008-12-03T10:29:46Z")

</div>

Hello,

Let's say that we have the two following arrays  
@array1=[1,2,4,6,7,8]  
@array2=[1,2,3,5,6,7]

Is there any easy way to compare these two arrays and print the values that exist in array1 and not in array2 and the values that exist in array2 and not in array1?

Regards,  
Chriss\_58

---

<div class="post-metadata">

**Author:** ![SFNYC](https://community.unix.com/letter_avatar/sfnyc/32/6_22734026db12803ebb3e059622ac3534.png) [@SFNYC](https://community.unix.com/u/SFNYC)\
**Post date:** [December 3, 2008, 11:16am UTC](https://community.unix.com/t/compare-arrays-in-perl/222929/2 "2008-12-03T11:16:56Z")

</div>

I found something in the Perl Cookbook that may help you.

```nohighlight
$ cat ./compare_array.pl
#!/usr/bin/perl

use strict;
use warnings;

my @array1;
my @array2;
my @diff;
my @isect;
my $item;
my %count;

@array1 = (1, 2, 4, 6, 7, 8);
@array2 = (1, 2, 3, 5, 6, 7);

@isect = ( );
@diff = ( );
%count = ( );

foreach $item (@array1, @array2) { $count{$item}++;}

foreach $item (keys %count) {
    if ($count{$item} == 2) {
        push @isect, $item;
    } else {
        push @diff, $item;
    }
}

print "\nA Array = @array1\n";

print "\nB Array = @array2\n";

print "\nIntersect Array = @isect\n";

print "\nDiff Array = @diff\n\n";

exit 0;

$ ./compare_array.pl

A Array = 1 2 4 6 7 8

B Array = 1 2 3 5 6 7

Intersect Array = 6 1 7 2

Diff Array = 8 4 3 5

```

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [December 3, 2008, 11:28am UTC](https://community.unix.com/t/compare-arrays-in-perl/222929/3 "2008-12-03T11:28:04Z")

</div>

Something like this?

```nohighlight
$ perl -le'
@array1=(1,2,4,6,7,8);
@array2=(1,2,3,5,6,7);

  map $count{$_}++ , @array1, @array2;

  $, = ",";

  print grep $count{$_} == 1, @array1, @array2;
' 
4,8,3,5

```

---

<div class="post-metadata">

**Author:** ![KevinADC](https://community.unix.com/letter_avatar/kevinadc/32/6_22734026db12803ebb3e059622ac3534.png) [@KevinADC](https://community.unix.com/u/KevinADC)\
**Post date:** [December 3, 2008, 2:08pm UTC](https://community.unix.com/t/compare-arrays-in-perl/222929/4 "2008-12-03T14:08:12Z")

</div>

```nohighlight
@array1=(1,2,4,6,7,8);
@array2=(1,2,3,5,6,7);

for $i (@array1) {
   print "$i\n" if ! grep {$i == $_} @array2;
}

```
