tr and y/// Is equivalent . To implement one-to-one mapping , But there are also additional features , Like Linux Next tr The order is the same .

usage :
tr/SEARCH/REPLACEMENT/cdsr y/SEARCH/REPLACEMENT/cdsr among :
c: take search Complement of , take search Replace all characters not found in with replacement Last character of d: delete search Characters that appear in
s: Compress repeating characters , Just compress and replace , You can replacement Specify as empty
r: The quantity returned is not the quantity successfully replaced , Instead, replace the content after success , and s/// Of r The modifiers are the same
for example :

1. Mapping function

Make lowercase letters e Replace with capital letters E.
$str="abcdef"; $str =~ y/e/E/; print "$str\n";
Replace all lowercase letters with uppercase letters .
$str="abcdef"; $str =~ y/[a-z]/[A-Z]/; print "$str\n";
If you specify a different mapping set for the same letter , Then the first mapping will take effect .
$str="aaa ddd eee fff"; $str =~ tr/aaa/xyz/; print "$str\n"; # output xxx ddd eee
fff
2. use r Return the result after replacement

This modifier makes it possible to copy data before processing it , Then operate on the replica data , So the raw data will remain the same .
$str="abcdef"; print $str =~ y/e/E/r; print $str,"\n";
therefore , You can r Operation result of modifier is assigned to other variables .
$str="abcde"; $copy = $str =~ y/e/E/r; print $copy,"\n"; print $str,"\n";
3. use s Modifier packed character
$str="abc ddd eee ffff"; print $str =~ tr/ //sr;
Line breaks can be compressed :
$str1="abc\n\nddd\neee fff"; print $str1 =~ tr/\n //sr;
4.d Modifier delete search Among them , but replacement Characters not in

That's retention replacement Characters of , Delete the rest .

for example , Delete some characters directly , Do not specify replacement, that search All characters in are removed .
$str="abc ddd eee fff"; $str =~ y/de//d; print qq("$str"); # output :"abc fff"
$str="abc ddd eee fff"; $str =~ y/de/e/d; # e Reserved print qq("$str");

about d Modifier , It's a little complicated . If search Some of the characters specified are not exact , When compiled, you don't know which characters need to be found , It's impossible to replacement Which characters to keep found in , thus replacement Part becomes redundant .
$str="abc ddd eee fff"; $str =~ y/[d-f]/e/d; # e Will not be retained , obtain "abc " $str =~
y/[def]/e/d; # e Will not be retained $str =~ y/[df]e/e/d; # e Will not be retained
5.c Modifier complement , take search Replace all characters not specified in with replacement Last character in
$str="aaa bbb ccc ddd"; print $str =~ y/ab/xy/cr; # aaaybbbyyyyyyyy
Pay attention to the top , except a and b External , All replaced with y character ,x Character ignored .

If replacement than search long , Then it is still taken replacement As a replacement character . So the following equivalence :
y/ab/xy/c; y/ab/zxy/c
therefore , Specify the replacement character of a complement .

If you also specify s Modifier , After complement replacement , Recompression .
$str="aaa bbb ccc ddd"; print $str =~ y/ab/xy/scr; # aaaybbby
If specified d Modifier , Delete all not in searchlist Characters in , in other words replacement Part becomes redundant :
$str="aaa bbb ccc ddd"; print $str =~ y/ab/xy/dcr; # aaabbb
6.c and d Modifier used together , One take search Complement reservation of search, A delete search, How to deal with ?

perl Of y/// It's reserved search, But no complement replacement , that is cd Part of each modifier . So ,replacement Part is redundant .
$str="abc ddd eee fff"; $str =~ y/df/x/cd; print qq("$str"); # return :dddfff