in reply to How To Do This Better?

Boy, you guys work hard.

if you are sucking from file, try this one:

$/ = undef; $line = <FILE>; close FILE; $line =~ s/\W+//g; print "I found ", length( $line ), "characters in the file\n";
which will count letters ( okay, it will include underscores and numbers too ). Remove the substitution for a character count. For exactly letters, use:
$line =~ s/[^a-zA-Z]+//g;
Just another way to do it,
Mik
Mik Firestone ( perlus bigotus maximus )

Replies are listed 'Best First'.
RE: Re: How To Do This Better?
by chromatic (Archbishop) on Apr 15, 2000 at 01:36 UTC
    That has the unfortunate consequence of modifying the original string. Easily worked around, but a limitation that ought to be pointed out. Besides that, it only counts the number of letters total, not the number of occurrences of each letter.

    In the spirit of Not Working Very Hard, here's another option to do what you suggest:

    $string = "abc1235ABC"; $number = ($string =~ tr[a-zA-Z][a-zA-Z]); print "I counted $number alphabetical characters.\n"; print "My string is still ->$string<-\n";
    (can't believe I forgot about that one)