CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Counting a string

 



abarabas
New User

Mar 24, 2005, 4:10 PM

Post #1 of 3 (688 views)
Counting a string Can't Post

Hi, How can I count the number of letters that has a string? Is it possible? Thx, sorry for my bad englishWink.


KevinR
Veteran


Mar 24, 2005, 4:26 PM

Post #2 of 3 (686 views)
Re: [abarabas] Counting a string [In reply to] Can't Post

you can count the number of characters in a string by using the length() function:

$number = length($string);

but that counts everything.

If you only wanted to count letters, as in the alphabet (a-z) this will work:

$count++ while $string =~ /[a-zA-Z]/g;

I believe this also works and might be faster:

$count = ($string =~ tr/a-zA-Z//);
-------------------------------------------------


abarabas
New User

Mar 24, 2005, 5:03 PM

Post #3 of 3 (685 views)
Re: [KevinR] Counting a string [In reply to] Can't Post

Thx, I'll try

Edit: Thanks! It worked!


(This post was edited by abarabas on Mar 24, 2005, 9:11 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives