### Re: Comparison by position and value

by ambrus (Abbot)
 on Jan 02, 2005 at 11:08 UTC ( #418771=note: print w/replies, xml ) Need Help??

in reply to Comparison by position and value

```sub compatible {
my (\$s1, \$s2) = @_;
y/_/\0/c for \$s1, \$s2;
!((\$s1 | \$s2)=~y/\0//);
}
print(compatible("_8__3__19", "48_____7_") ? "true\n" : "false\n");

Update: BrowserUK has pointed out that this is wrong. Indeed, it does not work if there is the same digit in both positions of the same string.

Update: Corrected (I hope) code

```sub compatible {
y/_/\477/c, y/_/\0/ for my @m = @_;
!(((\$_[0] ^ \$_[1]) & \$m[0] & \$m[1]) =~ y/\0//c);
}

printf "%s v %s ? %s\n", @\$_, compatible( @\$_ ) ? 1 : 0
for [ qw[ _8__3__19 48____7__ ] ],  # compat
[ qw[ _8__3__19 4_8___7__ ] ],  # compat
[ qw[ _8__3__19 48_____7_ ] ];  # clash

Update: Oh, I've got it wrong again. forget this.

Replies are listed 'Best First'.
Re^2: Comparison by position and value
by BrowserUk (Pope) on Jan 02, 2005 at 11:21 UTC

That doesn't work as is?

```#! perl -slw
use strict;

sub compatible {
my (\$s1, \$s2) = @_;
y/_/\0/c for \$s1, \$s2;
!((\$s1 | \$s2)=~y/\0//);
}

printf "%s v %s ? %s\n", @\$_, compatible( @\$_ ) ? 1 : 0
for [ qw[ _8__3__19 48____7__ ] ],  # good
[ qw[ _8__3__19 4_8___7__ ] ],  # bad
[ qw[ _8__3__19 48_____7_ ] ];  # bad

__END__
[11:18:29.65] P:\test>test
_8__3__19 v 48____7__ ? 0
_8__3__19 v 4_8___7__ ? 1
_8__3__19 v 48_____7_ ? 0

Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

```_8__3__19 v
48____7__ ? 0
^

_8__3__19 v
4_8___7__ ? 1

_8__3__19 v
48_____7_ ? 0
^     ^

For these strings at least, the result is 0 iff there is a clash of two digits somewhere.

The first two strings are compatible, because they only overlap (contain a digit in the same place) where the digit is the same in both strings. Please see the 3 examples in the OP.

Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

Create A New User
Node Status?
node history
Node Type: note [id://418771]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
As of 2020-10-31 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favourite web site is:

Results (291 votes). Check out past polls.

Notices?