in reply to Re: Counting how many times a character is in a string.
in thread Counting how many times a character is in a string.

All of the examples up to this point have been destructive though.

Wrong.

Quoting perlop on tr:

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 is replicated till it is long enough. If the REPLACEMENTLIST is empty, the SEARCHLIST is replicated. This latter is useful for counting characters in a class or for squashing character sequences in a class.

(Underline mine.)

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^3: Counting how many times a character is in a string.
by bart (Canon) on Nov 19, 2005 at 14:36 UTC
    If the REPLACEMENTLIST is empty, the SEARCHLIST is replicated. This latter is useful for counting characters in a class
    Furthermore, this used to effectively replace characters by themselves, causing it to fail on read-only values, but that has been fixed. Now tr/%// merely counts.