0

I am new in Perl programming. I am trying to compare the two arrays each element. So here is my code:

#!/usr/bin/perl
use strict;
use warnings;
use v5.10.1;

my @x = ("tom","john","michell");
my @y = ("tom","john","michell","robert","ricky");
if (@x ~~ @y)
{
    say "elements matched"; 
}
else 
{ 
    say "no elements matched";
}

When I run this I get the output no elements matched

So I want to compare both array elements in deep and the element do not matches, those elements I want to store it in a new array. As I can now compare the only matched elements but I can't store it in a new array.

How can I store those unmatched elements in a new array?

Please someone can help me and advice.

5
  • 1
    @array1 ~~ @array2 checks if the each index matches the same index in the other array. As perlop documentations says, it's like ($array1[0] ~~ $array2[0]) && ($array1[1] ~~ $array2[1]) && ... etc. Ultimately, your else statement is wrong, and should really say "some or all elements do not match". The only way to do what you want is to create a lookup table (hash) or iterate one array over the other... Both of which have already been provided as answers. Commented Aug 12, 2015 at 9:26
  • Array::Compare is ideal for your problem. search.cpan.org/~davecross/Array-Compare-2.11/lib/Array/… It is capable of listing out non-matched elements. Commented Aug 12, 2015 at 10:36
  • To clarify your question... are you wanting to compare the same index across both arrays? That is... does it matter if tom is in $x[0] but in $y[1] ... OR ... must tom be in $x[0] and $y[0] ? Commented Aug 12, 2015 at 11:42
  • no i am not comparing both index.. Commented Aug 12, 2015 at 11:45
  • You should avoid using smart matching. It is an experimental feature that is a bit broken and is highly likely to change in future versions of Perl Commented Aug 12, 2015 at 12:38

2 Answers 2

5

I'd avoid smart matching in Perl - e.g. see here

If you're trying to compare the contents of $y[0] with $x[0] then this is one way to go, which puts all non-matches in an new array @keep:

use strict;
use warnings;
use feature qw/say/;

my @x = qw(tom john michell);
my @y = qw(tom john michell robert ricky);

my @keep;
for (my $i = 0; $i <$#y; $i++) {
    unless ($y[$i] eq $x[$i]){
        push @keep, $y[$i];
    }
}

say for @keep;

Or, if you simply want to see if one name exists in the other array (and aren't interested in directly comparing elements), use two hashes:

my (%x, %y);

$x{$_}++ for @x;
$y{$_}++ for @y;

foreach (keys %y){
    say if not exists $x{$_};
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your comment for my answer. I not clearly read the question. Then i tried negative smart match is not work.
In the first way i can get only one element..whereas i suppose to get 2 elements?
That's because there are an unequal number of elements in the two arrays.
1

It would be well worth your while spending some time reading the Perl FAQ.

Perl FAQ 4 concerns Data Manipulation and includes the following question and answer:

How do I compute the difference of two arrays? How do I compute the intersection of two arrays?

Use a hash. Here's code to do both and more. It assumes that each element is unique in a given array:

my (@union, @intersection, @difference);
my %count = ();
foreach my $element (@array1, @array2) { $count{$element}++ }
foreach my $element (keys %count) {
    push @union, $element;
    push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element;
}

Note that this is the symmetric difference, that is, all elements in either A or in B but not in both. Think of it as an xor operation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.