The Regular Expression tr/// operator is used to change a set of characters into another set of characters as its not possible with "s//" operator, in this the second argument has replacement characters and only at compile time it checks the strings to be replaced.One can append "d", "c", "s" to add different functionality to the "tr" operator.
If the "d" option is used it delete's found but unreplaced characters, if "c" is appended complements or inverts the searchlist,"s" option is appended to squash duplicate replaced characters.
Example :
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$string = 'maurice mike is the father of morris mike';
$string2 = 'maurice mike';
$string =~ tr/abcde/ABCDE/;
print $string;
print "<br>";
$string2 =~ tr/a-z/x/;
print $string2;
Result :
mAuriCE mikE is thE fAthEr of morris mikE
In the above tr operator example first the lower case "abcde" is replaced to uppercase "ABCDE" where ever it is found.
Example :
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$string2 = 'maurice mike';
$string2 =~ tr/a-z/x/;
print $string2;
Result :
xxxxxxx xxxx
In the above tr/// operator example length of the replacement character "x" is 1. So "x" is repeated to the length of the string "string2".
Appending "d" to "tr" operator:
To remove the characters which are not in the matching list can be done by appending "d" to the end of the "tr" operator.
Example :
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$string = 'my name is sam anderson';
$string =~ tr/a-z/ABCDE/;
print $string;
Result :
EE EAEE EE EAE AEDEEEEE
In the above tr operator example all the characters are replaced with the uppercase "ABCDE", if the no matching characters are found by default it replaced by "E".
Example :
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$string2 = 'my name is sam anderson';
$string2 =~ tr/a-z/ABCDE/d;
print $string2;
Result :
AE A ADE
In the tr/// operator example when appended with "d" not macthing characters in the string is removed, and only the matching character replaced with its uppercase is displayed.
Appending "c" to "tr" operator:
To complement the string with respect to all 256 characters, any character specified in the string is removed then the remaining characters are taken to match the pattern.
Example :
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$string = 'my name is sam anderson';
$string =~ tr/a-z/_/c;
print $string;
Result :
my_name_is_sam_anderson
In the above tr operator example the "tr" operator leaves "a-z" in the string as it uses "c" operator, only other character left in the string is the blank space, so the replacement character "_" is replaced to all the blank spaces.
Appending "s" to "tr" operator:
To replace duplicate characters with the charater specified is done by appending the "s" to the end of the "tr" operator.
Example :
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
my $text = 'good freeze';
$text =~ tr/eo/eu/s;
print "$textn";
Result :
gud freze
In the above tr/// operator example the letters "o" and "e" is repeated twice, so by appending "s", "o" is replaced with "u" once and "e" with "e" again once and displays the result.