Here is an equality function for arrays of strings, takes references to two arrays as argument:

sub aeq { my ($first, $second, @comp) = @_; return 0 if @{$first} != @{$second}; @comp = grep {$first->[$_] ne $second->[$_]} 0..$#$first; return not @comp; }
We check to see that the lengths are the same, return false ( not equal) if they are not. Then we use grep over the indexes to scan for unequal elements, putting the index in another array for each mismatch. Finally we return the negation of the mismath array's length as result; no mismatches gave a zero length array.

Update: Thanks to gmax for spotting my blunder in obtaining the last index. Fixed. For kappa's concern, I was thinking more of avoiding a run off the end of $second if it was shorter. For efficiency, I'd write this in terms of a flipflop, .. in scalar context. On reflection, I'd either do that, or in list context return an array of indexes that match:

sub aeq { my ($first, $second, @comp) = @_; return 0 if @{$first} != @{$second}; @comp = grep {$first->[$_] eq $second->[$_]} 0..$#$first; wantarray ? @comp : @comp == @$first; }
The choice is, as you say, between efficiency and general utility. Pick efficiency if you never need to filter for matches.

U2 Bah, I'm suffering from a little brain damage, repaired the newer code.

After Compline,
Zaxo


In reply to Re: comparing arrays by Zaxo
in thread comparing arrays by cyberconte

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.