in reply to Tell whether two strings differ at only one position
It counts the number of non-NULLs produced by XORing the strings together. If that number is less than 2, then the strings were either identical or differed by only one character. This could also be written as:sub compare { (($_[0] ^ $_[1]) =~ tr/\0//c) < 2; }
The regex ensures the XORed string is either entirely NULLs or has just one non-NULL in it.sub compare { ($_[0] ^ $_[1]) =~ /^\0*(?:[^\0]\0*)?$/ }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Tell whether two strings differ at only one position
by rg0now (Chaplain) on Aug 04, 2005 at 19:20 UTC | |
by ikegami (Patriarch) on Aug 04, 2005 at 21:16 UTC | |
by rg0now (Chaplain) on Aug 05, 2005 at 09:59 UTC | |
|
Re^2: Tell whether two strings differ at only one position
by blazar (Canon) on Aug 05, 2005 at 09:12 UTC | |
by BrowserUk (Patriarch) on Aug 05, 2005 at 09:31 UTC |