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.

Syntax: tr/SearchList/ReplacementList/

Return: the translated string

Example 1: This example uses tr operator for translating from lower case to upper case.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
   
# Initializing the strings
$string1 = 'gfg is a computer science portal';
$string2 = 'geeksforgeeks';
  
# Calling tr function
$string1 =~ tr/a-z/A-Z/;
$string2 =~ tr/a-z/A/;
  
# Printing the translated strings
print "$string1\n";
print "$string2\n";

chevron_right



Output:



GFG IS A COMPUTER SCIENCE PORTAL
AAAAAAAAAAAAA

Example 2: This example uses tr operator for translating from upper case to lower case.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
   
# Initializing the strings
$string1 = 'GFG IS A COMPUTER SCIENCE PORTAL';
$string2 = 'GEEKSFORGEEKS';
  
# Calling tr function
$string1 =~ tr/A-Z/a-z/;
$string2 =~ tr/A-Z/p/;
  
# Printing translated strings
print "$string1\n";
print "$string2\n";

chevron_right


Output :

gfg is a computer science portal
ppppppppppppp

Example 3: This example uses tr operator for translating from upper case to numeric form.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
   
# Initializing the strings
$string1 = 'GFG IS A COMPUTER SCIENCE PORTAL';
$string2 = 'GEEKSFORGEEKS';
  
# Calling tr function
$string1 =~ tr/A-Z/0-9/;
$string2 =~ tr/A-Z/5/;
  
# Printing translated strings
print "$string1\n";
print "$string2\n";

chevron_right


Output :

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.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
   
# Initializing the strings
$string1 = 'geeksforgeeks';
$string2 = 'geeksforgeeks';
  
# Calling tr function
# without appending 's' to 'tr' operator
$string1 =~ tr/ee/e/;
  
# Appending 's' to 'tr' operator
$string2 =~ tr/ee/e/s;
  
# Printing translated strings
print "$string1\n";
print "$string2\n";

chevron_right


Output :

geeksforgeeks
geksforgeks

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.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
   
# Initializing the strings
$string1 = 'gfg is a computer science portal';
$string2 = 'gfg is a computer science portal';
  
# Calling tr function
# without appending 'c' to 'tr' operator
$string1 =~ tr/a-z/_/;
  
# Appending 'c' to 'tr' operator
$string2 =~ tr/a-z/_/c;
  
# Printing translated strings
print "$string1\n";
print "$string2\n";

chevron_right


Output :

___ __ _ ________ _______ ______
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 1: This tr operator does the task of lc() function and uc() function as well as it translates the input characters into numeric form etc.

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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.