perl function tr
The tr function in Perl is used for character translation or transliteration. It takes two sets of characters and replaces the characters in the first set with corresponding characters in the second set. The syntax of the tr function is:
tr/SEARCHLIST/REPLACEMENTLIST/cdsr
Here,
SEARCHLISTis the set of characters to be searched forREPLACEMENTLISTis the set of characters that will replace the searched charactersc,d,s, andrare optional modifiers.cstands for complement,dstands for delete,sstands for squeeze, andrstands for return.
Here is an example that shows how the tr function works:
my $string = "hello world"; $string =~ tr/ol/OL/; # replaces all "o" and "l" with "O" and "L" print $string; # Output: heLLO wOrLd
In this example, the tr function replaces all "o" and "l" characters in the $string variable with "O" and "L" respectively. The output is "heLLo wOrLd".
