Perl | tr Operator
The tr operator in Perl translates all characters of SearchList into the corresponding characters of ReplacementList.
Here the SearchList is the given input characters which are to be converted into the corresponding characters given in the ReplacementList.
Return:number of characters replaced or deleted
Example 1: This example uses tr operator for translating from lower case to upper case.
GFG IS A COMPUTER SCIENCE PORTAL AAAAAAAAAAAAA
Example 2: This example uses tr operator for translating from upper case to lower case.
gfg is a computer science portal ppppppppppppp
Example 3: This example uses tr operator for translating from upper case to numeric form.
656 89 0 29999949 9284924 999909 5555555555555
Use of ‘s’ operator :
The ‘s’ operator is used at the end of ‘tr’ operator to detect the duplicate characters in the given string and replace them with the specified character.
In the above code, it can be seen that when ‘s’ is appended to the end of ‘tr’ operator then it replaces repeated “e” i.e, “ee” with a single “e”.
Use of ‘c’ operator :
The ‘c’ operator is used at the end of ‘tr’ operator to detect the space (‘ ‘) character in the given string and replace it with the specified character.
___ __ _ ________ _______ ______ gfg_is_a_computer_science_portal
In the above code, it can be seen that in between each word the sign underscore (“_”) is used which is done with the appending of ‘c’.
Note 2: This tr operator is slightly different from y operator because y operator does not use the appending of “c” or “s” to the operator but tr operator does.