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...

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...