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

In reply to Re: comparison of two arrays by kabeldag
in thread comparison of two arrays by lax

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.