in reply to Compare two strings of same length, character-by-character

It's a little crude, but:

my $x = $str1 ^ $str2; $x =~ s/[^\0]+//g; print length($x);

Replies are listed 'Best First'.
Re^2: Compare two strings of same length, character-by-character (feature 'bitwise')
by LanX (Saint) on Nov 28, 2023 at 22:38 UTC
    See also Bitwise-String-Operators for more background

    And please note the effects of use feature 'bitwise' and especially use v5.28

    (I seemed to remember that bitwise deprecated this behavior, but it's rather adding an extra operator ^. to avoid ambiguities.)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

Re^2: Compare two strings of same length, character-by-character
by cavac (Prior) on Nov 30, 2023 at 15:04 UTC

    Beware that binary comparison of strings does not work on Unicode. Even length() doesn't work. Well, it depends what type of comparison you want.

    If you want to compare if the strings are exactly the same bit-by-bit, your stuff works. If you need to compare if the characters are functionally equivalent, that's a different matter.

    As discussed before, many Unicode characters can be encoded in more than one way, see Re^2: incorrect length of strings with diphthongs

    Further, even if you have one character that's exactly the same Unicode character in both strings, they still might be displayed completely different. Aside from the Umlaut stuff, you have other modifiers as well, like skin tone for Emoji, right-to-left stuff. And also there some scripts like Arab where the display of a character changes depending on the characters around it.

    And what's extra nice about about modifier characters is that it may or may not modify your debug prints:

    my $modifier = chr(0x200F); print "The character $modifier is only in string 1\n";

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP