On Dec 13, Uri Guttman said:

>if i ever saw someone getting a string length in real code with tr/// i
>would kick them hard. on the other hand counting the number of newlines
>in a string is a valid use of that effect. in fact there is no other
>simple and fast way to count newlines in a string but tr///.

Two comments.  First, it's already been noted that y!!!c is shorter than
length by one character, so if you have a string in $_, it's "best" to use
y!!!c. ;)  Second, a la FAQ:

========================================================================
=head1 Found in /usr/local/lib/perl5/5.00502/pod/perlfaq4.pod

=head2 How can I count the number of occurrences of a substring within a
string?

There are a number of ways, with varying efficiency: If you want a
count of a certain single character (X) within a string, you can use the
C<tr///> function like so:

    $string = "ThisXlineXhasXsomeXx'sXinXit":
    $count = ($string =~ tr/X//);
    print "There are $count X charcters in the string";
========================================================================

So the FAQ itself sanctions (in the positive sense of the word) the use of
tr/// for counting characters, and uses the empty-RHS approch.

Oh, and see my signature for the sed-virgins among us.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.

Reply via email to