Perl tr/// operator
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:
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
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
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/;
# it prints: ONE TWO THREE FOUR FIVE SIX SEVEN
This code will convert the content of the $string
variable to uppercase.
Through these exercises you have the opportunity to try yourself to write some script code where you can use the Perl tr/// operator. These exercises are completely covered in my Perl "Perl Scalar and String Functions - How To Tutorial"
where I show you how to play with this important function in detail.
|1. ||Use tr/// to convert all the elements of the array @array = qw(one two three four five six seven) into uppercase.|
|2. ||Use tr/// with the complement (c) modifier to convert the following string: '123.95,44.32,27.77,221q23' into '123 95 44 32 27 77 221 23'.|
|3. ||Use tr/// with the delete (d) modifier to convert the following string: 'aa bbb c dd eee ff mmmm rr' into '11 222 3 44'.|
|4. ||Use tr/// with the squash (s) modifier to convert the following string: '123,,,95,,44,,32,,,,27....77' into '123,95,44,32,27,77'.|
|5. ||Use tr/// with more than one modifier, for example to convert the string: 'This . is an 7example 9only' into ' This-is-an-example-only '.|
|6. ||Give an example about how to use variables, either in SEARCHLIST or in REPLACEMENTLIST of the tr/// operator.|
|7. ||Use tr/// to count the number of dots within the following string: 'This pencil is .... old. You can ..... it .....'.|
|8. ||Specify a few differences between tr/// and s///.|
Table of Contents:
A Perl Script
Perl Data Types
Built-in Perl Functions
Functions by Category
Regular Expressions and Pattern Matching
Functions in alphabetical order
return from Perl tr/// function to Perl Basics
Would you like to create your own website like this one?
Hit the Alarm Clock!