(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?


In reply to Re^2: How to make this substitutions without splitting the strings? (tr/// behavior) by AnomalousMonk
in thread How to make this substitutions without splitting the strings? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.