The Perl tr/// function is used to transliterate all occurrences found in a search list with the corresponding character in a replacement list. This function is a character by character translation and if you want to perform more complex operations you can use the s/// regex substitution operator instead. In Perl tr/// is known better as an operator rather than a function.
The syntax form of the tr/// operator is as follows:
tr/SEARCHLIST/REPLACEMENTLIST/cds
It replaces all the occurrences of the characters in SEARCHLIST with the characters in the REPLACEMENTLIST. It returns the number of characters replaced or deleted. The transliteration table is built at compile time, so you can’t use interpolation either in SEARCHLIST or in REPLACEMENTLIST. If you need to use variables, you can use the eval function.The c, d, s are modifiers that add additional functionality to the Perl tr/// operator:
- c - is used to specify that the SEARCHLIST character set is complemented
- d - is used to delete found but not replaced characters
- s - is used to specify that the sequences of characters that were transliterated to the same character are squashed down to a single instance of the character
The strings can be specified via the =~, !~ binding operators or $_ by default. Please note that Perl tr/// operator does not do regular expressions character classes such as \d.
One character that has a special meaning to tr/// is the dash (-) and it indicates a range of characters (for example tr/A-C/a-c/).The slash (/) has the meaning of a delimiter, because it separates the arguments of the tr/// operator. However, you can replace it with whatever sign of punctuation you want. The syntax changes a little if you use parentheses because they include the idea of containment.
For instance, the following syntax forms of the Perl tr/// operator are equivalents and do the same thing (it replaces any lowercase character from a to f with digits from 2 to 7 and any character from g to z with the 7 digit):
$str =~ tr/a-z/2-7/;
$str =~ tr:a-z:2-7:;
$str =~ tr[a-z][2-7];
$str =~ tr(a-z)(2-7);
$str =~ tr<2-7>;
However, if you work with Unicode characters and you need to change your text in lowercase or uppercase, don’t use the tr/// operator because it not pays attention to Unicode information. In this case you can use the \U sequence in a double quote string or the equivalent lc and uc functions.To illustrate how to use this operator, look at the following example which shows you how you can use the tr/// operator wih the =~ binding operator:
my $string = 'one two three four five six seven';
$string =~ tr/a-z/A-Z/;
print "$string\n";
# it prints: ONE TWO THREE FOUR FIVE SIX SEVEN
This code will convert the content of the $string variable to uppercase.
Check my new How To Tutorial eBook (PDF format):to see a lot of fully commented examples that help you use the tr/// function in your scripts.
NEW!!!
Do you want more information about the basic Perl topics?
Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :