 | PERL: comaprying an array with the permuation of another arr |  |
|
priya
|
 |
Posted: Tue Jul 25, 2006 9:03 pm |
|
 |
 |
 |
 |
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 |
|
 |
Posted: Thu Jul 27, 2006 11:53 am |
|
 |
 |
 |
 |
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 |
|
 |
Posted: Fri Mar 30, 2007 9:25 pm |
|
 |
 |
 |
 |
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
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
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
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
|
 |
Posted: Tue Apr 03, 2007 9:52 am |
|
 |
 |
 |
 |
the above is why Ruby pwns Perl any day of the week. this should work:
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:
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?
|
|
|
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
|
|
|
|
|