On Jul 19, Mooney Christophe-CMOONEY1 said:

>"That's an excellent question," i said to myself; "i'll bet there's a
>module for that!"
>
>So, i looked on cpan, and it looks like Array::Compare will do the
>trick.

All the same, this is a question that stumps a lot of people.  There are
two ways to approach this, really -- one way is terribly broken and takes
longer than it needs, and the other is fast and simple.

The first method is to join each array into a single string, and compare
the two.  How does that break?

  @a = ("a b", "c");
  @b = ("a", "b c");

  if (join(' ', @a) eq join(' ', @b)) {
    print "\@a and \@b are equal!\n";
  }

See why that breaks?  (If not, run it.)  "So," you say, "you just need to
find a unique string!"  That requires going through both arrays to find a
string that is not found in ANY element -- that's a LOT of work, and it
requires you to loop over the arrays.  Why not COMPARE the elements AS you
loop?

That is method two:

  sub compare {
    my ($aref, $bref) = @_;
    return 0 if @$aref != @$bref;  # ensure the same size
    $aref->[$_] ne $bref->[$_] and return for 0 .. $#$aref;
    return 1;
  }

It is called thus:

  if (compare(\@this, \@that)) { 
    # the same
  }

The dissection of that for loop is:

  for my $idx (0 .. $#$aref) {
    if ($aref->[$idx] ne $bref->[$idx]) { return 0 }
  }

I hope this helps.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
**      Manning Publications, Co, is publishing my Perl Regex book      **


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to