
Another way to mangle strings with Perl is using the tr/// operator.
This is the text transliteration operator - it doesn't translate your English into Mandarin.
Similar to the substitution s/// operator, it replaces all the characters found in the first list with the corresponding character in the second list.
Say what?
Transliteration is like a finer-tooth comb. Instead of replacing one string with another, it replaces each LETTER with another LETTER.
$text = "\nMy name's Tim.\n"; $text =~ tr/i/o/; print "\n$text\n";
![]()
Of course you could achieve the same result by merely swapping the tr for s, so here's a better example.
$text="ABCDEF"; print"\n\$text: $text"; $text=~ tr/abcDEF/0/; print "\nComplemented \$text: $text\n\n";
![]()
This did NOT find 'a', 'b', or 'c' so left them alone. But it DID find 'D', 'E', and 'F' and 'transliterated' them with '0', '0', and '0'.
There are also modifiers that can be used with tr///:
That's complements NOT compliments.
Regex 5 - Quantifiers