Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Tell whether two strings differ at only one position

by rg0now (Chaplain)
on Aug 04, 2005 at 19:20 UTC ( #480994=note: print w/ replies, xml ) Need Help??


in reply to Re: Tell whether two strings differ at only one position
in thread Tell whether two strings differ at only one position

Wow! japhy++

sub compare { (($_[0] ^ $_[1]) =~ tr/\0//c) < 2; }
While I liked each and every solution other wise monks have been so kind to come up with so far, when I looked at the solution of japhy, I immediately saw that this is the one I have been searching for! It is clean and concise and perlish, and, quite amazingly, it is just one statement, so I can eliminate the compare sub alltogether which, as I hope, will make my script even faster!

I am beginning to see that after a certain point, Perl programming becomes a matter of pure aesthetics...


Comment on Re^2: Tell whether two strings differ at only one position
Select or Download Code
Replies are listed 'Best First'.
Re^3: Tell whether two strings differ at only one position
by ikegami (Pope) on Aug 04, 2005 at 21:16 UTC
    Except it doesn't work:
    sub compare { (($_[0] ^ $_[1]) =~ tr/\0//c) < 2; } print((compare('abc', 'abcd') ? 'ok' : 'not ok'), "\n");
    It should be
    sub compare { length $_[0] == length $_[1] && (($_[0] ^ $_[1]) =~ tr/\0//c) < 2; }
      Thanks ikegami for pointing this one little important fact out to me! I have almost forgot my very own specifications for the problem. That tr///c absolutely made my day...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://480994]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (12)
As of 2015-07-31 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (276 votes), past polls