more useful options | |
PerlMonks |
Re: string comparisonby robartes (Priest) |
on May 27, 2005 at 07:16 UTC ( [id://460965]=note: print w/replies, xml ) | Need Help?? |
Here's one, naieve and probably inefficient, solution:
Never mind the inefficient loop within loop within loop. The important bit to this method is splitting the strings into a hash, then walking over one hash and deleting the keys that exist in the other. If anything is left, the strings do not match. One quick optimisation is to immediately stop and declare inequality when you find that a key in one hash does not exist in the other - that will save you iterating over the entire hash in the worst case. Update: Check both hashes after delete step. See reply to this post from whatluo as to why. Note that the more efficient solution to this would be as whatluo suggests -- report inequality when the number of keys does not match between the two hashes before going into the delete loop. The solution I offer leaves the differing keys in the hashes, so you can do something with them. CU
In Section
Seekers of Perl Wisdom
|
|