Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How many match 1

Status
Not open for further replies.

audiopro

Programmer
Joined
Apr 1, 2004
Messages
3,165
Location
GB
I have a simple match to test if a string contains contains the letter 's' which works ok.
Code:
if($_[0]=~ m/@/){
code
}
How can I count how many times 's' appears in that string?

Keith
 
Code:
my $count = $_[0] =~ /s/;

When called in scalar context, a regular expression returns the number of times matched. Called in array context, it returns all of the values captured (if there were any; you need parenthesis and stuff to capture values).

-------------
Cuvou.com | The NEW Kirsle.net
 
A little off with the code Kirsle:

Code:
$test = 'sssfffsss';
my $count = () = $test =~ /s/g;
print $count;

or a liitle more easier and efficient:

Code:
$test = 'sssfffsss';
my $count = $test =~ tr/s/s/;
print $count;


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks for that, I'll give it a try tomorrow.
Could please you point me to a tutorial on matching and subbing, preferably one written slowly because I don't read very fast.


Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top