Question

I have found the following piece of Perl code online:

y/a-z//s

I looked in the docs to see what it does, but I didn't find anything about it. What does the y stand for here?

The full code:

($_='jjjuuusssttt annootthheer
     pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;
Was it helpful?

Solution

According to perlop:

tr/SEARCHLIST/REPLACEMENTLIST/cdsr
y/SEARCHLIST/REPLACEMENTLIST/cdsr
Transliterates all occurrences of the characters found in the search list with the corresponding character in the replacement list.

For sed devotees, y is provided as a synonym for tr.

Options:

  • c Complement the SEARCHLIST.
  • d Delete found but unreplaced characters.
  • s Squash duplicate replaced characters.
  • r Return the modified string and leave the original string untouched.

If the REPLACEMENTLIST is empty, the SEARCHLIST is replicated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top