my %count = (); foreach my $device (@current_list, @temp_devicelist) { $count{$device}++; } #### my %count; # ... for ($n=0; $n < $size; $n++) { our $device=$current_list[$n][0]; $count{$device}++; } # ... for ($n=0; $n < $size; $n++) { our $tempdevicelinks=$temp_list[$n][0]; $count{$tempdevicelinks}++; } # ... my @difference = grep { $count{$_} == 1 } keys %count; my @intersect = grep { $count{$_} == 2 } keys %count; my @union = keys %count; #### print "Difference:\n"; print "$_\n" for @difference; print "Intersect:\n"; print "$_\n" for @intersect; print "Union:\n"; print "$_\n" for @union;