Re: comparison of two arrays
by prasadbabu (Prior) on Jun 16, 2006 at 08:59 UTC
|
use strict;
use warnings;
use List::Compare::Functional qw(:originals :aliases);
my @array1 = ('anu','abu','ali');
my @array2 = ('anu','abu', 'ali');
my @c = get_complement( [ \@array1, \@array2 ] ); #data present in the
+ @array2 but not present in @array1
(@c) ? print "mismatch, return (0)\n" : print "no mismatch here\n"; #
+return here itself if mismatch in first array
my @d = get_unique( [ \@array1, \@array2 ] ); #data present in the @ar
+ray1 but not present in @array2
(@d) ? print "mismatch, return (0)\n" : print "no mismatch in both ar
+rays\n"; #return if mismatch
updated: rodion++, thanks :)
| [reply] [d/l] |
|
|
Also, if you care about speed, it's a good idea to check if the number of elements in the two arrays are not equal, and return early.
| [reply] |
Re: comparison of two arrays
by davorg (Chancellor) on Jun 16, 2006 at 10:02 UTC
|
| [reply] |
Re: comparison of two arrays
by McDarren (Abbot) on Jun 16, 2006 at 09:07 UTC
|
Howdy lax,
We have a comprehensive Q&A section here, including a whole list of array-related Questions and Answers.
I can guarantee you that you're not the first one to ask this (or a similar) question, so I suggest you check the above out :)
| [reply] |
Re: comparison of two arrays
by kabeldag (Hermit) on Jun 16, 2006 at 11:24 UTC
|
Ok. Well here is a way to do what you asked. Try for an exact 'element for element' array match.
Why use a third-party module for an array compare ??
use strict;
my @arrayone = ('anu','abu','ali');
my @arraytwo = ('anu','abu');
my $arrays_match_result=match_arrays(\@arrayone,\@arraytwo);
if($arrays_match_result==1) {
print "Array's are the same\n";
}else{
print "Array's are not the same\n";
}
sub match_arrays {
my ($array1,$array2)=@_;
my $el2cnt=0;
my $not_the_same;
for my $el1 (@$array1) {
unless ($el1 eq @$array2[$el2cnt]) {
$not_the_same=1;
}
$el2cnt++;
}
if($not_the_same==1) {
return 0;
}else{
return 1;
}
}
| [reply] [d/l] |
|
|
hi
thanks a lot for your solution
i would also like to add the foll: condition to mi question
the above solution does not seem to evaluate the arrays in which the elements are of different order but contain the same elements
so will i be able to get a solution which will evaluate the arrays in which the elements are of different order but contain the same elements and "return true" .
The criteria is if two arrays contain the same elements even if they are not in different order the function should return true
Sorry! for not mentioning this condition in mi previous question
Thanks
lax
| [reply] |
|
|
If you're only interested in matching what the elements are and not their order, all you have to do is copy them into a pair of sorted temporary arrays and then compare that those are identical. Something like:
sub compare_arrays {
my ($array1, $array2) = @_;
my @temp1 = sort @$array1;
my @temp2 = sort @$array2;
return 0 unless $#temp1 = $#temp2; # If number of elements is differ
+ent, they don't match
for my $i (0..$#temp1) {
return 0 unless $temp1[$i] eq $temp2[$i]; # Something's different
+, no match
}
return 1; # OK, they match.
}
(Note: Not tested.) | [reply] [d/l] |
|
|
hi
How do i do an exact element to element match
kindly help me
thanks
lax
| [reply] |
Re: comparison of two arrays
by TedPride (Priest) on Jun 17, 2006 at 01:45 UTC
|
If you want to check for content but not order, and would rather have more speed (for larger numbers of items) at the cost of a bit more memory:
use strict;
use warnings;
my @array1 = ('anu','abu','ali');
my @array2 = ('anu','abu');
print mycmp(\@array1, \@array2);
sub mycmp {
my ($a1, $a2) = @_;
return 0 if $#$a1 != $#$a2;
my %h;
$h{$_}++ for @$a1;
$h{$_}-- for @$a2;
for (values %h) {
return 0 if $_;
}
return 1;
}
| [reply] [d/l] |
|
|
hi
Thanks a lot for your excellent code
it really works
thanks®ards
lax
| [reply] |
Re: comparison of two arrays
by kabeldag (Hermit) on Jun 17, 2006 at 02:57 UTC
|
Well. Let's open a box/element from array1, and check the box's/elements in array2. If the box's are the same, cross them off the list and don't worry about them again.
use strict;
my @arrayone = ('a','b','c','d');
my @arraytwo = ('c','d','a','b');
my $arrays_match_result=match_arrays(\@arrayone,\@arraytwo);
if($arrays_match_result==1) {
print "Array's contain the same element value's.\n";
}else{
print "Array's do not contain the same element value's.\n";
}
sub match_arrays {
my ($array1,$array2)=@_;
my @array1_matches;
my @array2_matches;
my $matches=0;
# Return 0 straight away,
# if respective array lengths are not equal
unless ($#$array1==$#$array2) {
return 0;
}
for(my $mc1=0;$mc1<=$#$array1;$mc1++) {
for(my $mc2=0;$mc2<=$#$array2;$mc2++) {
# If the elements match. Eliminate the
# respective matched elements
# cross out the box's with the same contents)
# and don't worry about them again.
if(@$array1[$mc1] eq @$array2[$mc2]&&$array2_matches[$mc2]
+!=1&&$array1_matches[$mc1]!=1) {
$array2_matches[$mc2]=1;
$array1_matches[$mc1]=1;
$matches++;
print "Match[$matches] -> Array1 : Element: $mc1 (@$ar
+ray1[$mc1]) matched Array2 : Element: $mc2 (@$array2[$mc2])\n";
}
}
}
if($matches==$#$array1+1) {
return 1;
}else{
return 0;
}
}
| [reply] [d/l] |