Reply to topic
PERL: comaprying an array with the permuation of another arr
priya


Joined: 25 Jul 2006
Posts: 3
Reply with quote
Hi there,

Does anyone know how to compare an array in perl with another array ? I mean I want to compare the array with the permutation of another array. For example,

@a = {2,1,3};
@b={1,3,2}; // any one of the permuation of this array should match with @a.
@c = {5,4,2};

When I compare @a with @b, it should return true where as when I compare @a with @c, it should return false. I really need to know this.

Thank you in advance,
priya
spoulson


Joined: 21 Jun 2006
Posts: 22
Location: Middletown, DE
Reply with quote
The simple way would be to first sort the array before comparing.

But, my question is, how are you comparing? Are you doing a "if (@a == @b)"? That wouldn't work.

I found this thread on PerlMonks that would give you some ideas on many ways to handle an array comparison.
http://perlmonks.org/?node_id=335021[/img]
spandox


Joined: 03 Jul 2004
Posts: 17
Location: Delaware
Reply with quote
Code:

sub compArray
# compare two arrays
# use: compArray(\@one,\@two)
# returns 1 for same 0 for different
# stupid obfuscated version
# optimised for speed - no local vars
{
   if (@{$_[0]} != @{$_[1]}) { return 0; } # test lengths
   foreach (0 .. $#{$_[0]}) # go element by element
   {
     if ($_[0][$_] ne $_[1][$_]) {return 0;}
   }
   return 1;  # If I got here we are good
}




Example how to use

Code:

my @a = (1,2,3);
my @b = (1,2,3);
if ( compArray(\@a,\@b )){ print "True\n";}else{print "False\n";}


NOTE THIS WILL NOT WORK WITH RECURSIVE STUFF

Slight mod will work

Code:
sub compArrayRec
{
   if (@{$_[0]} != @{$_[1]}) { return 0; } # test lengths
   foreach (0 .. $#{$_[0]}) # go element by element
   {
     if (UNIVERSAL::isa($_[0][$_],"ARRAY") and
         UNIVERSAL::isa($_[1][$_],"ARRAY") )
         )
     {
        if (!compArrayRec($_[0][$_],$_[1][$_])){return 0;}
     }
     else
     {
          if ($_[0][$_] ne $_[1][$_]) {return 0;}
     }
   }
   return 1;
}



and usage

Code:

my @a = (1,2,3,[1,2,3]);
my @b = (1,2,3,[1,2,3]);
if ( compArrayRec(\@a,\@b )){ print "True\n";}else{print "False\n";}


I leave it up to the reader to figure it out for Hashes too
pmeserve
HostMySite Tech

Joined: 19 Mar 2004
Posts: 178
Reply with quote
the above is why Ruby pwns Perl any day of the week. this should work:
Code:
class Array
   def lazy_eql?(a)
      raise ArgumentError unless a.is_a? Array # verify we got an array
               
      # verify number of elements, then merge & check again
      return (a.length == self.length && (a | self).length == self.length)
   end
end


Let's see:
Code:
t = ["a", "b", "c"]
t1 = ["a", "b", "b"]
t2 = ["c", "b", "a"]

t.lazy_eql? t1
  => false
t.lazy_eql? t2
  => true


Now which code do you want to be responsible for maintaining?
PERL: comaprying an array with the permuation of another arr
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic