Purl said:
$string = "131313131";
$string =~ tr/1-9/A-I/;
print $string;
The perl statement
tr/3/C/;
clearly will translate all occurrances of "3" to "C",
regardless of where the characters appear in the string.
The perl statement
tr/1/A/;
clearly will translate all occurances of "1" to "A",
regardless of where they characters appear in the string.
For the perl statement
tr/31/CA/;
two translations occur. All occurances of "1" get translated
to "A", not just the ones that immediately follow a "3"
(which is what the s/// operator would do).
For
tr/123456789/ABCDEFGHI/;
all occurrances of "2" get translated to "B", regardless of
whether they follow a "1" or preceed "3" or not.
If we can agree on that, then there should not be any problem
in understanding that
tr/123456789/ABCDEFGHI/;
and
tr/1-9/A-I/;
are identical (based on how "-" is interpeted by the tr/// function.
You expect processing to
occur in string position order, not according to
numerical value.
That is both correct and incorrect.
/1-9/ is the string of all the characters whose ASCII codes range from
31 to 39 decimal ("123456789").
/A-I/ is the string of all the characters whose ASCII codes range from
65 to 73 decimal ("ABCDEFGHI").
So the string position order in these examples is based on the numerical
value of the character's ASCII code. This is true for both digit and
non-digit characters.
The tr/// function is documented to work in string position order.
That makes it very handy to use in performing ROT13 encoding.
tr/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm/;
is the same as tr/A-MN-Za-mn-z/N-ZA-Mn-za-m/;
and is the same as tr/A-Za-z/N-ZA-Mn-za-m/;.
The latter two use sequences of start_character, hyphen, end_character to
indicate consecutive characters in the ASCII code. All three of them
translate one set of 52 characters to a different set of 52 characters.
Since tr/// does work in string position order, you would expect
tr/369258147/CFIBEHADG/;
to operate exactly the same was as
tr/123456789/ABCDEFGHI/;
and it does.
-Joe