Home: Perl Programming Help: Intermediate:
Counting Spaces



andy7t
User

Aug 20, 2003, 1:15 PM


Views: 1069
Counting Spaces

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


Views: 1066
Re: [andy7t] Counting Spaces

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


davorg
Thaumaturge / Moderator

Aug 21, 2003, 1:13 AM


Views: 1062
Re: [andy7t] Counting Spaces


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