Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

tr

Performs character-by-character transliteration on a string.

Transliterates characters in the target string according to a precompiled translation table. Each character found in the search list is replaced by the corresponding character in the replacement list.

The target string is obtained from the targ pad slot, the stack, or $_. For tr/// (OP_TRANS), the string is modified in place and the count of translated characters is pushed. For tr///r (OP_TRANSR), a new modified copy is pushed instead.

Modifiers (encoded in op_private):

  • /c (0x20): Complement the search list
  • /d (0x80): Delete found but unreplaced characters
  • /s (0x08): Squash duplicate replaced characters

Synopsis

$str =~ tr/searchlist/replacementlist/;
$str =~ y/a-z/A-Z/;          # y is a synonym
my $count = ($s =~ tr/a//);   # count occurrences
my $new   = $s =~ tr/a-z/A-Z/r;  # /r returns copy

See Also

s///, m//, perlop