in reply to comparison of two arrays

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; } }

Replies are listed 'Best First'.
Re^2: comparison of two arrays
by lax (Initiate) on Jun 16, 2006 at 13:01 UTC
    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
      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.)
Re^2: comparison of two arrays
by lax (Initiate) on Jun 16, 2006 at 14:04 UTC
    hi
    How do i do an exact element to element match
    kindly help me

    thanks
    lax