perl-5.38.0
    tr/SEARCHLIST/REPLACEMENTLIST/cdsr

    Transliterates all occurrences of the characters found (or not found if the /c modifier is specified) in the search list with the positionally corresponding character in the replacement list, possibly deleting some, depending on the modifiers specified. It returns the number of characters replaced or deleted. If no string is specified via the =~ or !~ operator, the $_ string is transliterated.

    検索リスト (SEARCHLIST) に含まれる (あるいは /c 修飾子が指定されている場合は含まれない)文字を、 置換リスト (REPLACEMENTLIST) の位置的に対応する文字に変換します (指定された修飾子によっては一部が削除されることもあります)。 置換または削除が行なわれた、文字数を返します。 =~ 演算子や !~ 演算子で文字列が指定されていなければ、$_ の文字列が 変換されます。

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

    sed 愛好者のために、ytr の同義語として提供されています。

    If the /r (non-destructive) option is present, a new copy of the string is made and its characters transliterated, and this copy is returned no matter whether it was modified or not: the original string is always left unchanged. The new copy is always a plain string, even if the input string is an object or a tied variable.

    /r (非破壊) オプションがあると、文字列の新しいコピーが作られてその 文字が変換され、変更されたかどうかに関わらずこのコピーが返されます: 元の文字列は常に無変更で残されます。 新しいコピーは、たとえ入力がオブジェクトや tie された変数でも、常に プレーンな文字列です。

    Unless the /r option is used, the string specified with =~ must be a scalar variable, an array element, a hash element, or an assignment to one of those; in other words, an lvalue.

    /r オプションが使われない限り、=~ で指定される文字列は、スカラ変数、 配列要素、ハッシュ要素、あるいはこれらへの代入式といった左辺値で なければなりません。

    The characters delimitting SEARCHLIST and REPLACEMENTLIST can be any printable character, not just forward slashes. If they are single quotes (tr'SEARCHLIST'REPLACEMENTLIST'), the only interpolation is removal of \ from pairs of \\; so hyphens are interpreted literally rather than specifying a character range.

    SEARCHLISTREPLACEMENTLIST を区切る文字は、 スラッシュだけでなく、任意の表示文字が可能です。 それがシングルクォート (tr'SEARCHLIST'REPLACEMENTLIST') の場合、 \\ の組から \ を削除する変換のみが行われます; したがって、 ハイフンは文字の範囲ではなく、リテラルに解釈されます。

    Otherwise, a character range may be specified with a hyphen, so tr/A-J/0-9/ does the same replacement as tr/ACEGIBDFHJ/0246813579/.

    さもなければ、文字の範囲はハイフンを使って指定できます; tr/A-J/0-9/tr/ACEGIBDFHJ/0246813579/ と同じ置換を行います。

    If the SEARCHLIST is delimited by bracketing quotes, the REPLACEMENTLIST must have its own pair of quotes, which may or may not be bracketing quotes; for example, tr(aeiouy)(yuoiea) or tr[+\-*/]"ABCD". This final example shows a way to visually clarify what is going on for people who are more familiar with regular expression patterns than with tr, and who may think forward slash delimiters imply that tr is more like a regular expression pattern than it actually is. (Another option might be to use tr[...][...].)

    SEARCHLIST をかっこ類で括った場合には、REPLACEMENTLIST 用に、 もう一組の区切り文字を用意しなければなりません; これは、括弧類であっても、なくてもかまいません; 例えば、tr(aeiouy)(yuoiea)tr[+\-*/]"ABCD" です。 最後の例は、tr よりも正規表現パターンに親しんでいる人や、 前のスラッシュ区切り文字は、tr が実際以上に正規表現パターンに 似ていることを暗示していると考える人に、何が起きているかを 視覚的に明確化しています。 (もう一つの選択肢は tr[...][...] を使うことかもしれません。)

    tr isn't fully like bracketed character classes, just (significantly) more like them than it is to full patterns. For example, characters appearing more than once in either list behave differently here than in patterns, and tr lists do not allow backslashed character classes such as \d or \pL, nor variable interpolation, so "$" and "@" are always treated as literals.

    tr は完全に大かっこ文字クラスと同様ではなく、 単に完全なパターンよりは(かなり)そちらにより似ていると言うだけです。 例えば、どちらかのリストに複数回文字が現れた場合、パターンと 異なる振る舞いをします; そして tr の一覧は \d\pL といった逆スラッシュ文字クラスや 変数展開を許さないので、"$""@" は 常にリテラルとして扱われます。

    The allowed elements are literals plus \' (meaning a single quote). If the delimiters aren't single quotes, also allowed are any of the escape sequences accepted in double-quoted strings. Escape sequence details are in the table near the beginning of this section.

    許される要素はリテラルと (シングルクォートを意味する) \' です。 区切り文字がシングルクォートでない場合 ダブルクォート文字列の中で受け入れられるエスケープシーケンスも 許されます。 エスケープシーケンスの詳細は この節の先頭付近の表 にあります。

    A hyphen at the beginning or end, or preceded by a backslash is also always considered a literal. Precede a delimiter character with a backslash to allow it.

    先頭、末尾、および逆スラッシュが前置されたハイフンもまた常に リテラルとして扱われます。 それをするために逆スラッシュと共に区切り文字を前に置きます。

    The tr operator is not equivalent to the tr(1) utility. tr[a-z][A-Z] will uppercase the 26 letters "a" through "z", but for case changing not confined to ASCII, use lc, uc, lcfirst, ucfirst (all documented in perlfunc), or the substitution operator s/PATTERN/REPLACEMENT/ (with \U, \u, \L, and \l string-interpolation escapes in the REPLACEMENT portion).

    tr 演算子は tr(1) ユーティリティと等価ではありません。 tr[a-z][A-Z] は "a" から "z" までの 26 文字を大文字にしますが、 ASCII の範囲外の大文字小文字を変更する場合は、 lc, uc, lcfirst, ucfirst (全て perlfunc に文書化されています), あるいは 置換演算子 s/PATTERN/REPLACEMENT/ (REPLACEMENT 部での \U, \u, \L, \l 文字列変換エスケープ) を 使ってください。

    Most ranges are unportable between character sets, but certain ones signal Perl to do special handling to make them portable. There are two classes of portable ranges. The first are any subsets of the ranges A-Z, a-z, and 0-9, when expressed as literal characters.

    ほとんどの範囲は文字集合管で互換性がありませんが、一部のものは互換性を 持たせるために Perl が特別に扱います。 移植性のある範囲には二つのクラスがあります。 一つ目はリテラル文字として記述された A-Z, a-z, 0-9 の部分集合です。

      tr/h-k/H-K/

    capitalizes the letters "h", "i", "j", and "k" and nothing else, no matter what the platform's character set is. In contrast, all of

    とすると、プラットフォームの文字集合が何であるかに関わらず、 文字 "h", "i", "j", "k" を大文字にして、それ以外は何もしません。 一方、次のようなもの全ては

      tr/\x68-\x6B/\x48-\x4B/
      tr/h-\x6B/H-\x4B/
      tr/\x68-k/\x48-K/

    do the same capitalizations as the previous example when run on ASCII platforms, but something completely different on EBCDIC ones.

    ASCII プラットフォームで実行した場合は前述の例と同じ大文字かを行いますが、 EBCDIC プラットフォームでは全く違うことをします。

    The second class of portable ranges is invoked when one or both of the range's end points are expressed as \N{...}

    移植性のある範囲の二つ目のクラスは、範囲の片方または両方の端が \N{...} として記述された場合です

     $string =~ tr/\N{U+20}-\N{U+7E}//d;

    removes from $string all the platform's characters which are equivalent to any of Unicode U+0020, U+0021, ... U+007D, U+007E. This is a portable range, and has the same effect on every platform it is run on. In this example, these are the ASCII printable characters. So after this is run, $string has only controls and characters which have no ASCII equivalents.

    これは、Unicode での U+0020, U+0021, ... U+007D, U+007E と等価な、 今のプラットフォームでの全ての文字を $string から削除します。 これは移植性のある範囲で、実行される全てのプラットフォームで同じ効果が あります。 この例では、これらは ASCII の表示文字です。 従ってこれを実行した後、$string は制御文字および ASCII で等価なものがない 文字のみとなります。

    But, even for portable ranges, it is not generally obvious what is included without having to look things up in the manual. A sound principle is to use only ranges that both begin from, and end at, either ASCII alphabetics of equal case (b-e, B-E), or digits (1-4). Anything else is unclear (and unportable unless \N{...} is used). If in doubt, spell out the character sets in full.

    しかし、移植性のある範囲であっても、 マニュアルで調べる必要なしには、何が含まれるのかは 一般的に明らかではありません。 健全な原則としては、範囲の最初と最後をどちらも ASCII 英字 (大文字小文字も同じ)(b-e, B-E)にするか、どちらも数字にする(1-4) ことです。 それ以外は不明確です(そして \N{...} が使われていない限り 移植性はありません)。 疑わしいときは、文字セットを完全に書き出してください。

    Options:

    オプションは以下の通りです:

        c   Complement the SEARCHLIST.
        d   Delete found but unreplaced characters.
        r   Return the modified string and leave the original string
            untouched.
        s   Squash duplicate replaced characters.
        c   SEARCHLIST を補集合にする。
        d   見つかったが置換されなかった文字を削除する。
        r   修正した結果を返し、もとの文字列はそのままにする。
        s   置換された文字が重なったときに圧縮する。

    If the /d modifier is specified, any characters specified by SEARCHLIST not found in REPLACEMENTLIST are deleted. (Note that this is slightly more flexible than the behavior of some tr programs, which delete anything they find in the SEARCHLIST, period.)

    /d 修飾子が指定されると、SEARCHLIST に指定されて、 REPLACEMENTLIST に対応するものがない文字が削除されます。 (これは、SEARCHLIST で見つかったものを削除する、ただそれだけの、ある種の tr プログラムの動作よりと比べれば、いく分柔軟なものになっています。)

    If the /s modifier is specified, sequences of characters, all in a row, that were transliterated to the same character are squashed down to a single instance of that character.

    /s 修飾子が指定されると、同じ文字に文字変換された文字の並びを、 その文字 1 文字だけに圧縮します。

     my $a = "aaabbbca";
     $a =~ tr/ab/dd/s;     # $a now is "dcd"

    If the /d modifier is used, the REPLACEMENTLIST is always interpreted exactly as specified. Otherwise, if the REPLACEMENTLIST is shorter than the SEARCHLIST, the final character, if any, is replicated until it is long enough. There won't be a final character if and only if the REPLACEMENTLIST is empty, in which case REPLACEMENTLIST is copied from SEARCHLIST. An empty REPLACEMENTLIST is useful for counting characters in a class, or for squashing character sequences in a class.

    /d 修飾子が使われると、REPLACEMENTLIST は、常に指定された通りに 解釈されます。 さもなければ、REPLACEMENTLISTSEARCHLIST よりも短い場合、 同じ長さになるまで、最後の文字が繰り返されているものとして扱われます。 REPLACEMENTLIST が空文字列のときには最後の文字というものはなく、 その場合は REPLACEMENTLISTSEARCHLIST からコピーされます。 空の REPLACEMENTLIST は、ある文字クラスに含まれる文字数を数えるときや、 ある文字クラスの文字の並びを圧縮するようなときに便利です。

        tr/abcd//            tr/abcd/abcd/
        tr/abcd/AB/          tr/abcd/ABBB/
        tr/abcd//d           s/[abcd]//g
        tr/abcd/AB/d         (tr/ab/AB/ + s/[cd]//g)  - but run together

    If the /c modifier is specified, the characters to be transliterated are the ones NOT in SEARCHLIST, that is, it is complemented. If /d and/or /s are also specified, they apply to the complemented SEARCHLIST. Recall, that if REPLACEMENTLIST is empty (except under /d) a copy of SEARCHLIST is used instead. That copy is made after complementing under /c. SEARCHLIST is sorted by code point order after complementing, and any REPLACEMENTLIST is applied to that sorted result. This means that under /c, the order of the characters specified in SEARCHLIST is irrelevant. This can lead to different results on EBCDIC systems if REPLACEMENTLIST contains more than one character, hence it is generally non-portable to use /c with such a REPLACEMENTLIST.

    /c 修飾子が指定されると、SEARCHLIST に「ない」ものが文字変換されます; つまり、補集合です。 /d/s も指定されている場合、これらは SEARCHLIST の補集合に 適用されます。 REPLACEMENTLIST が空の場合、(/d の下でなければ) 代わりに SEARCHLIST のコピーが使われることを思い出してください。 このコピーは、/c の下の補集合化の後で行われます。 SEARCHLIST は補集合化の後で符号位置の順序でソートされ、 REPLACEMENTLIST はソートされた結果に対して適用されます。 つまり、/c の下では、SEARCHLIST で指定された文字の順序は 無関係です。 REPLACEMENTLIST に複数の文字が含まれている場合、 EBCDIC システムでは異なった結果を引き起こすことがあるので、 そのような REPLACEMENTLIST に対して /c を使うのは一般的に移植性がありません。

    Another way of describing the operation is this: If /c is specified, the SEARCHLIST is sorted by code point order, then complemented. If REPLACEMENTLIST is empty and /d is not specified, REPLACEMENTLIST is replaced by a copy of SEARCHLIST (as modified under /c), and these potentially modified lists are used as the basis for what follows. Any character in the target string that isn't in SEARCHLIST is passed through unchanged. Every other character in the target string is replaced by the character in REPLACEMENTLIST that positionally corresponds to its mate in SEARCHLIST, except that under /s, the 2nd and following characters are squeezed out in a sequence of characters in a row that all translate to the same character. If SEARCHLIST is longer than REPLACEMENTLIST, characters in the target string that match a character in SEARCHLIST that doesn't have a correspondence in REPLACEMENTLIST are either deleted from the target string if /d is specified; or replaced by the final character in REPLACEMENTLIST if /d isn't specified.

    この操作を説明するもう一つの方法は次のものです: /c が指定されると、SEARCHLIST は符号位置順にソートされ、 それから補集合化されます。 REPLACEMENTLIST が空で /d が指定されていないと、 REPLACEMENTLIST は (/c で修正された) SEARCHLIST のコピーに 置き換えられ、この変更されているかも知れないリストは引き続くものに対しての 基礎として使われます。 ターゲット文字列のうち、SEARCHLIST ににない文字は変更されずに そのままになります。 ターゲット文字列のその他のそれぞれの文字は、 SEARCHLIST の中の文字に位置的に対応する、 REPLACEMENTLIST の中の文字に置き換えられます; /s の下では例外で、 連続して同じ文字に変換された文字並びの 2 文字目以降の文字は削除されます。 SEARCHLISTREPLACEMENTLIST より長い場合、 SEARCHLIST の文字のうち REPLACEMENTLIST に対応するものが ない文字にマッチングしたターゲット文字列の中の文字は、 /d が指定されていればターゲット文字列から削除されます; /d が指定されていなければREPLACEMENTLIST の最後の文字に 置き換えられます。

    Some examples:

    いくつかの例:

     $ARGV[1] =~ tr/A-Z/a-z/;   # canonicalize to lower case ASCII
    
     $cnt = tr/*/*/;            # count the stars in $_
     $cnt = tr/*//;             # same thing
    
     $cnt = $sky =~ tr/*/*/;    # count the stars in $sky
     $cnt = $sky =~ tr/*//;     # same thing
    
     $cnt = $sky =~ tr/*//c;    # count all the non-stars in $sky
     $cnt = $sky =~ tr/*/*/c;   # same, but transliterate each non-star
                                # into a star, leaving the already-stars
                                # alone.  Afterwards, everything in $sky
                                # is a star.
    
     $cnt = tr/0-9//;           # count the ASCII digits in $_
    
     tr/a-zA-Z//s;              # bookkeeper -> bokeper
     tr/o/o/s;                  # bookkeeper -> bokkeeper
     tr/oe/oe/s;                # bookkeeper -> bokkeper
     tr/oe//s;                  # bookkeeper -> bokkeper
     tr/oe/o/s;                 # bookkeeper -> bokkopor
    
     ($HOST = $host) =~ tr/a-z/A-Z/;
      $HOST = $host  =~ tr/a-z/A-Z/r; # same thing
    
     $HOST = $host =~ tr/a-z/A-Z/r   # chained with s///r
                   =~ s/:/ -p/r;
    
     tr/a-zA-Z/ /cs;                 # change non-alphas to single space
    
     @stripped = map tr/a-zA-Z/ /csr, @original;
                                     # /r with map
    
     tr [\200-\377]
        [\000-\177];                 # wickedly delete 8th bit
    
     $foo !~ tr/A/a/    # transliterate all the A's in $foo to 'a',
                        # return 0 if any were found and changed.
                        # Otherwise return 1

    If multiple transliterations are given for a character, only the first one is used:

    複数の文字変換が一つの文字について指定されると、最初のものだけが使われます。

     tr/AAA/XYZ/

    will transliterate any A to X.

    は A を X に変換します。

    Because the transliteration table is built at compile time, neither the SEARCHLIST nor the REPLACEMENTLIST are subjected to double quote interpolation. That means that if you want to use variables, you must use an eval():

    変換テーブルはコンパイル時に作られるので、SEARCHLISTREPLACEMENTLIST もダブルクォート展開の対象とはなりません。 変数を使いたい場合には、eval() を使わなければならないということです:

     eval "tr/$oldlist/$newlist/";
     die $@ if $@;
    
     eval "tr/$oldlist/$newlist/, 1" or die $@;