Updated March 28, 2023
Introduction to Perl tr
The Perl tr function is working as a translation operator in the required list of strings in the Perl technology. This function is a translation function using to translate the available list to the user required list. The tr function is useful for convert the string into numbers, an uppercase letter from the lowercase letter, and one string to another required string in the Perl language. The tr function replaces any type of list to the user required format list or replacement list using the tr operator.
Syntax
- The basic Perl tr function syntax is below.
tr/available - list/translate - list/
- Description:
- The “tr” operators are used for replacing the string with the required format of the string.
- The “available – list” in the search list is present in the Perl string variable.
- The “translate – list” is the new list that is replaced by the “available – list” in the Perl language.
- The Perl tr function with “s” operator syntax is below.
$ perl_tr_string_variable =~ tr/available - list/translate - list/s;
- Description:
- The “s” operator is found out the duplicate characters in the string and is replaced with the required character or list.
- The “=~” is the binding operator to translate the required list and keep the remaining character the same in the list.
- The “s” operator has placed the end of the basic tr function syntax.
- The remaining syntax is the same as the above basic tr function syntax.
- The Perl tr function with “c” operator syntax is below.
$ perl_tr_string_variable =~ tr/available - list/translate - list/c;
- Description:
- The “c” operator is finding out the space between the characters, and the user can replace space with the required character or format.
- The “c” operator has placed the end of the basic Perl tr function syntax.
- The remaining syntax is the same as the above basic Perl tr function syntax.
- The Perl tr function with “d” operator syntax is below.
$ perl_tr_string_variable =~ tr/available - list/translate - list/d;
- Description:
- The “d” operator is finding out how many number of characters were deleted previously.
- The “d” operator has placed the end of the basic tr function syntax.
- The remaining syntax is the same as the above basic tr function syntax.
How does the tr function work in Perl?
- The download the Perl software and install it in your operating system of the device.
https://www.Perl.org/ or http://strawberryPerl.com/ are commonly using Perl software websites.
- Make a file with the Perl extension in the device and save the file in the command line path.
Example: helloo.pl or first pearl.pl
- Create the string variable and initialize it with some characters or lists.
$ perl_tr_string_variable1 = "I am learning Perl language.";
$ perl_tr_string_variable2 = "i am learning Perl tr function.";
- The basic tr function syntax for translate the string in the Perl language.
$ perl_tr_string_variable1 =~ tr/a-z/A-z/;
- The user can use any operators like s, c, d for more convenience and usability.
- Here we are using the c operator to understand the working of the operator function.
$ perl_tr_string_variable2 =~ tr/a-z/*/c;
- The string can return the replaced list using the Perl string variable.
print "$ perl_tr_string_variable1 \n";
print "$ perl_tr_string_variable2 \n";
- The working sample of the tr function is below.
$ perl_tr_string_variable1 = "I am learning Perl language.";
$ perl_tr_string_variable2 = "i am learning Perl tr function.";
$ perl_tr_string_variable1 =~ tr/a-z/A-z/;
$ perl_tr_string_variable2 =~ tr/a-z/*/c;
print "$ perl_tr_string_variable1 \n";
print "$ perl_tr_string_variable2 \n";
Examples
Different examples are mentioned below:
Example #1
The basic tr function with Sentence case conversion example and output.
Code:
$ perl_tr_string_variable1 = "i am learning Perl language.";
$ perl_tr_string_variable2 = "I A LEARNING PERL TR FUNCTION...";
print "original string : $ perl_tr_string_variable1 \n";
$ perl_tr_string_variable1 =~ tr/a-z/A-z/;
print " lowercase to uppercase : $ perl_tr_string_variable1 \n";
print "original string : $ perl_tr_string_variable2 \n";
$ perl_tr_string_variable2 =~ tr/A-z/a-z/;
print " uppercase to lowercase : $ perl_tr_string_variable2 \n";
Output:
Example #2
The basic tr function with character to number conversion example and output.
Code:
$ perl_tr_string_variable1 = "i am learning technical language with online hub.";
$ perl_tr_string_variable2 = "1, 2, 3, 4, 5, 6";
print "original string1 : $ perl_tr_string_variable1 \n";
$ perl_tr_string_variable1 =~ tr/a-z/0-9/;
print " characters to numbers : $ perl_tr_string_variable1 \n";
print "original string2 : $ perl_tr_string_variable2 \n";
$ perl_tr_string_variable2 =~ tr/0-9/a-z/;
print " numbers to characters : $ perl_tr_string_variable2 \n";
Output:
Example #3
The basic tr function with “s” operator example and output.
Code:
$ perl_tr_string_variable1 = "ii aam lleeaarrniinngg nneeww llaannggaauuggee.";
$ perl_tr_string_variable2 = "ii aam lleeaarrniinngg nneeww llaannggaauuggee";
print "original string1 : $ perl_tr_string_variable1 \n";
$ perl_tr_string_variable1 =~ tr/a-z/a-z/;
print " string without s operator : $ perl_tr_string_variable1 \n";
print "original string2 : $ perl_tr_string_variable2 \n";
$ perl_tr_string_variable2 =~ tr/a-z/a-z/s;
print "string with s operator : $ perl_tr_string_variable2 \n";
Output:
Example #4
The basic tr function with “c” operator example and output.
Code:
$ perl_tr_string_variable1 = "i am learning Perl language.";
$ perl_tr_string_variable2 = "I A LEARNING PERL TR FUNCTION...";
print "original string1 : $ perl_tr_string_variable1 \n";
$ perl_tr_string_variable1 =~ tr/a-z/*/;
print " string without c operator : $ perl_tr_string_variable1 \n";
print "original string2 : $ perl_tr_string_variable2 \n";
$ perl_tr_string_variable2 =~ tr/A-Z/*/c;
print "string with c operator : $ perl_tr_string_variable2 \n";
Output:
Example #5
The basic tr function with “d” operator example and output.
Code:
$ perl_tr_string_variable1 = "i am learning Perl language.";
$ perl_tr_string_variable2 = "I A LEARNING PERL TR FUNCTION...";
print "original string1 : $ perl_tr_string_variable1 \n";
$ perl_tr_string_variable1 =~ tr/a-z/*/;
print " string without d operator : $ perl_tr_string_variable1 \n";
print "original string2 : $ perl_tr_string_variable2 \n";
$ perl_tr_string_variable2 =~ tr/A-Z/*/d;
print "string with d operator : $ perl_tr_string_variable2 \n";
Output:
Example #6
The basic tr function with case-sensitive example and output.
Code:
$ perl_tr_string_variable1 = "I Am Learning Perl Language...";
$ perl_tr_string_variable2 = "I AM Learning PERL TR Function...";
print "original string1 : $ perl_tr_string_variable1 \n";
$ perl_tr_string_variable1 =~ tr/a-z/t/;
print " string without d operator : $ perl_tr_string_variable1 \n";
print "original string2 : $ perl_tr_string_variable2 \n";
$ perl_tr_string_variable2 =~ tr/A-Z/T/;
print "string with d operator : $ perl_tr_string_variable2 \n";
Output:
Conclusion
- The tr function is useful for handle lists and modifies them as per the user’s requirement.
- The tr function makes a list user-friendly and easy to understand in the Perl language.
- The tr function is easy to change the given list into the user-required format.
Recommended Articles
This is a guide to Perl tr. Here we discuss How tr function works in Perl and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –