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: Intermediate:
Counting Spaces

 



andy7t
User

Aug 20, 2003, 1:15 PM

Post #1 of 3 (484 views)
Counting Spaces Can't Post

How do i count the number of spaces in a variable.

For example:

$phrase = "Some phrase with five spaces in";

Then if there are more than 2 spaces then give the user a warning such as "You are only allowed two spaces".


Paul
Enthusiast

Aug 20, 2003, 5:22 PM

Post #2 of 3 (481 views)
Re: [andy7t] Counting Spaces [In reply to] Can't Post

my $count = ($string =~ tr/ //);


davorg
Thaumaturge / Moderator

Aug 21, 2003, 1:13 AM

Post #3 of 3 (477 views)
Re: [andy7t] Counting Spaces [In reply to] Can't Post


Code
warn "You are only allowed two spaces." 
if $phrase =~ tr/ // > 2;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


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

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