in reply to Re: How to make this substitutions without splitting the strings?
in thread How to make this substitutions without splitting the strings?

(my $p = shift) =~ tr/-\0-\377/\0\377/;

In the  tr/-\0-\377/\0\377/ expression, the '-' (hyphen) character appears twice in the search list: initially, and also within the  \0-\377 range. In tests I did with some Win32 Perls in the range 5.8 to 5.14, the test code

c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'XXXooX'; (my $t = $s) =~ tr/XoX/ab/; print qq{'$t'}; " 'aaabba'
(and identically for  tr/X\x00-\xff/ab/) always produced the same result: the leftmost occurrence of a character in the search list is selected for matching to and replacement by the corresponding character in the replacement list.

I considered using the much neater  tr/-\0-\377/\0\377/ version, but I couldn't find anything in the docs to guarantee the behavior shown in my tests must always prevail. Despite the tests, I didn't feel comfortable using an "undocumented feature". Do you know of any documentation of this "leftmost match" feature in the  tr/// built-in? In a regex, the rule would be "leftmost longest match", but  tr/// isn't really a regex, it's a transliterator — isn't it?

Replies are listed 'Best First'.
Re^3: How to make this substitutions without splitting the strings? (tr/// behavior)
by farang (Chaplain) on Aug 01, 2014 at 05:22 UTC

    Do you know of any documentation of this "leftmost match" feature in the tr/// built-in?
    From perlop:

    "If multiple transliterations are given for a character, only the first one is used:

    tr/AAA/XYZ/

    will transliterate any A to X."

      Ah, the undocumented, documented! That feels better! I looked for just this sort of assertion (a couple of times) and couldn't find it. Thanks!