You shouldn't use grep in this context, because grep will search through an entire array no matter what--in other words, it'll do a lot of extra work. If you write up your own routine, you can break out of the compare loop as soon as you find two elements that aren't equal. This is good.

Use the code from the FAQ!

$are_equal = compare_arrays(\@frogs, \@toads); sub compare_arrays { my ($first, $second) = @_; local $^W = 0; # silence spurious -w undef complaints return 0 unless @$first == @$second; for (my $i = 0; $i < @$first; $i++) { return 0 if $first->[$i] ne $second->[$i]; } return 1; }
In particular, note this line:
return 0 unless @$first == @$second;
So the routine returns automatically if the arrays aren't of equal size. In your algorithm, you compare the elements in the arrays even if they're of different sizes. In fact, you explicitly wrote this in using your max function! Why? If the arrays aren't the same size, how could they possibly be equal?

Also, look at the FreezeThaw examples in the FAQ.


In reply to RE: Re: Comparing arrays by btrott
in thread Comparing arrays by Anonymous Monk

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.