I am trying to compare the content of two arrays and I need the final output as "Matched" or "Not Matched" I have written the below code and it is giving the expected output. However, can anyone suggest me any other simple way of doing it
#!/usr/bin/perl
use strict;
use warnings;
#Numeric scalar
my @array_1= (10,20,40,19);
my @array_2= (10,30,23,19);
print "@array_1\n";
my $count=0;
while ($count < scalar @array_1){
for (@array_2) {
if ($array_1[$count] == $array_2[$count]) {
print "matched\n";
$count++;}
else {
print "Not matched\n";
$count++;
}
}
}
Above solution is good. Also you can use https://metacpan.org/pod/Array::Compare module
Array::Compare - Perl extension for comparing arrays. If you have two arrays and you want to know if they are the same or different, then Array::Compare will be useful to you. All comparisons are carried out via a comparator object.
use strict;
use warnings;
use Array::Compare;
my @array_1= (10,20,40,19);
my @array_2= (10,30,23,19,66);
my $comp = Array::Compare->new;
if ($comp->compare(\@array_1, \@array_2)) {
print "Arrays are the same (Matched)\n";
} else {
print "Arrays are different (Not Matched)\n";
}
Output
Arrays are different (Not Matched)