in reply to Comparing hashes without sorting the keys
#!/usr/bin/perl/ use strict; my %aa=(uno=>1, due=>2); my %bb=(due=>2, uno=>1); my ($aa,$bb,@aa,@bb); **updated** @aa=sort(%aa); @bb=sort(%bb); $aa = join("",@aa); $bb = join("",@bb); **updated if ($aa eq $bb){ print "Matches!\n"; } else{ print "No match!\n"; }
Update: As both samtregar and <a href="http://www.perlmonks.org/index.pl?node_id=56270>greenFox point out the original code blows up with the data they show in their nodes. However replacing the
@aa=%aa; @bb=%bb; $aa = join("",sort @aa); $bb = join("", sort @bb);
with
@aa=sort(%aa); @bb=sort(%bb); $aa = join(" ",@aa); $bb = join(" ",@bb);
I believe should do what I originally intended.
Sorting the hash, not the array, and leaving the space in should solve the problem found by the other monks.
There is no emoticon for what I'm feeling now.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Comparing hashes without sorting the keys
by samtregar (Abbot) on Feb 18, 2004 at 23:25 UTC | |
by greenFox (Vicar) on Feb 19, 2004 at 04:17 UTC | |
by Popcorn Dave (Abbot) on Feb 19, 2004 at 17:30 UTC | |
|
Re: Re: Comparing hashes without sorting the keys
by samtregar (Abbot) on Feb 19, 2004 at 18:04 UTC | |
by Popcorn Dave (Abbot) on Feb 20, 2004 at 04:34 UTC |