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
(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.c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'XXXooX'; (my $t = $s) =~ tr/XoX/ab/; print qq{'$t'}; " 'aaabba'
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 | |
by AnomalousMonk (Archbishop) on Aug 01, 2014 at 05:34 UTC |