|Welcome to the Monastery|
Using variables with tr///by greenhorn (Sexton)
|on Jul 17, 2000 at 10:39 UTC||Need Help??|
greenhorn has asked for the wisdom of the Perl Monks concerning the following question:
I had it in mind to pass a string and a character to a subroutine, and within the subroutine, use a variable ("$char", below) containing the character as follows:
<kbd>$count = $string =~ tr/$char/$char/;</kbd>
Purpose: get a count of occurrences of "$char" within "$string".
In which case, tr/// happily counts occurrences of "c" and "h" and "a" and "r"...just as I told it to do. :) Can a variable be used in this instance? I realize that s/// can be used; it will be less efficient than tr///--so I understand--but it will return a count, given the right syntax.
Slightly off-topic: in perlop (ActiveState's, at least), the following appears in the section on tr///:
Aspiring technical writers everywhere, please study the above for a moment for an example of how not to write documentation--whether it's end-user documentation, or "only" reference material.</rant>