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.
@array1 ~~ @array2checks if the each index matches the same index in the other array. Asperlopdocumentations 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.tomis in$x[0]but in$y[1]... OR ... musttombe in$x[0]and$y[0]?