tr andy/// It is equivalent.. To implement one-to-one mapping, But there are also additional features, Just likeLinux Lowertr Same order.
tr/SEARCH/REPLACEMENT/cdsr y/SEARCH/REPLACEMENT/cdsr among：
c： takesearch Complement set, takesearch Replace all characters not found in withreplacement Last character of d： deletesearch Characters that appear in
s： Compress repeating characters, Just compress and replace, Can bereplacement Designated as empty
r： The quantity returned is not the quantity successfully replaced, Instead, replace the content after success, ands/// Ofr The modifiers are the same
1. Mapping function
Make lowercase letterse Replace with capital lettersE.
$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"; # outputxxx ddd eee
2. User 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, Can ber Operation result of modifier is assigned to other variables.
$str="abcde"; $copy = $str =~ y/e/E/r; print $copy,"\n"; print $str,"\n";
3. uses 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 deletesearch There are, butreplacement Characters not in
That's retentionreplacement Character, Delete the rest..
for example, Delete some characters directly, Not specifiedreplacement, thatsearch All characters in are deleted.
$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 Be retained print qq("$str");
aboutd Modifier, Somewhat complicated. Ifsearch Some of the characters specified are not exact, When compiled, you don't know which characters need to be found, It's impossible toreplacement Which characters to keep found in, thusreplacement 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, takesearch Replace all characters not specified in withreplacement Last character in
$str="aaa bbb ccc ddd"; print $str =~ y/ab/xy/cr; # aaaybbbyyyyyyyy
Notice the above, excepta andb External, All replaced withy character,x Character ignored.
Ifreplacement thansearch long, Then it is still takenreplacement As a replacement character. So the following equivalence：
therefore, Specify the replacement character of a complement.
If you also specifys Modifier, After complement replacement, Recompression.
$str="aaa bbb ccc ddd"; print $str =~ y/ab/xy/scr; # aaaybbby
If specifiedd Modifier, Delete all not insearchlist Characters in Chinese characters, In other wordsreplacement Part becomes redundant：
$str="aaa bbb ccc ddd"; print $str =~ y/ab/xy/dcr; # aaabbb
6.c andd Modifier used together, One takesearch Complement reservation ofsearch, A deletionsearch, How to deal with?
perl Ofy/// Is reservedsearch, But no complement replacement, that iscd Part of each modifier. Therefore,replacement Part is redundant.
$str="abc ddd eee fff"; $str =~ y/df/x/cd; print qq("$str"); # Return：dddfff