sub compare { return unless length $_[0] == length $_[1]; # Omit if expecting differences almost all the time. return 1 if $_[0] eq $_[1]; my $limit = 1; foreach (split('', $_[0]^$_[1])) { next unless ord; return 0 unless $limit--; } return 1; }
{ my @data = ( [ qw( abab abab ) ], [ qw( abab abaa ) ], [ qw( abab qrst ) ], [ qw( abab abba ) ], [ qw( abab ababa ) ], ); printf("%-5s - %-8s is %sOK\n", @$_, (compare(@$_)?'':'not ')) foreach @data; }
Doesn't work with Unicode.
In reply to Re: Tell whether two strings differ at only one position
by ikegami
in thread Tell whether two strings differ at only one position
by rg0now
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |