in reply to Using Array::Compare

Do you need to use a module to compare a list ?

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 equa +l 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; } }

Replies are listed 'Best First'.
Re^2: Using Array::Compare
by davorg (Chancellor) on Jul 05, 2006 at 12:59 UTC

    Well no. Of course you don't need a module to do it. You don't need a module to do anything in Perl. But if you want to reuse the code in a number of programs then it makes sense to put it a module. Which is what I did. And then I put it on CPAN in the hope that it would save people time as they wouldn't need to write those functions themselves.

    But, obviously, feel free to not use it :-)

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Hey.

      It's a great thing having bundled code. Don't get me wrong. I think it is good that you have created the module and I acknowlege the fact that you have. I was merely just posting a bit of code :).

      Just take off some of my XP ;') Good work on the module though.