my $count = () = $str =~ /a/g;
Thanks Chas. Owens,
I need some explanation on the above on how the above regex count the number
of 'a' in a string.
With my limited understanding, this is what I thought:-
$count = () ; #To me it means $count is assigned with a undefined value
which is then assigned with a value of $str
To me $str=~/a/g is a matching test, if it match it will be 1 and if
unmatched will be 0.
So when we assemble them together, how does it count?
Thanks
----- Original Message -----
From: "Chas. Owens" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Cc: <beginners@perl.org>
Sent: Sunday, April 13, 2008 12:40 PM
Subject: Re: how to construct regex to count no. of alphabets in a scalar
variable
On Sun, Apr 13, 2008 at 12:35 AM, <[EMAIL PROTECTED]> wrote:
Hi,
How do I construct a regex to count the number of 'a' within a scalar
variable such as the undermentioned.
One method I can think of is to split the scalar string into a list and
then do the counting but I just feel that this in not necessary.
snip
There are two good solutions that I am aware of. You should use the
first if you are counting single characters and the second otherwise:
my $count = $str =~ tr/a//;
my $count = () = $str =~ /a/g;
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/