in reply to comparison of two arrays
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; } }
|
|---|