The Tr (Translate) Operator
#!/usr/bin/perl -w
####################### The Translate Operator ###########################
$var = "aBcDeFg";
$var =~ tr/A-Z/a-z/; ###### Effectively like lc().
print "$var\n";
$var = "0A2#B!C";
$var =~ tr/A-Z/X/c; ###### Replace non-upcases with X (XAXXBXC).
print "$var\n";
$var = "0A2#B!C";
$var =~ tr/A-Z/X/cs; ###### Same as previous tr but makes XX become X.
print "$var\n";
$var = "12xyz45abc6"; ###### Delete all digits (xyzabc).
$var =~ tr/0-9//d;
print "$var\n";
$var = "12xyz45abc6"; ###### Delete all NON-digits (12456).
$var =~ tr/0-9//cd;
print "$var\n";
$_ = $var = "12xyz45abc6"; ###### Count of digits in $_ (5).
$newvar = tr/0-9//; ###### Notice we use "=" not "=~".
print "$newvar\n";
$newvar = $var =~ tr/0-9//; ###### Same thing but counts digits in $var.
print "$newvar\n";
$var = "0123456789";
$var =~ tr/0-9/W-Z/; ###### Right list padded by last char (Z).
print "$var\n"; ###### Result is WXYZZZZZZZ.
$var = "0123456789";
$var =~ tr/0-9/W-Z/d; ###### Translate but DELETE elements in left
print "$var\n"; ###### list that are not in the right (WXYZ).
$var = "0004444566662221"; ###### Squeeze identical consecutive digits.
$var =~ tr/0-9//s; ###### Result: 045621.
print "$var\n";
$var = "a\001bcd\007ef\017gh\007";
print "$var\n";
$var =~ tr/\001-\020//d; ###### You can use \n,\t,\f in tr but tr knows
print "$var\n"; ###### NOTHING about regular expressions!
####################### Results of Execution Below ######################
abcdefg
XAXXBXC
XAXBXC
xyzabc
12456
5
5
WXYZZZZZZZ
WXYZ
045621
abcdefgh
abcdefgh