in reply to Re: comparison of two arrays
in thread comparison of two arrays

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

Replies are listed 'Best First'.
Re^3: comparison of two arrays
by dsheroh (Monsignor) on Jun 16, 2006 at 14:56 UTC
    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.)