how to construct regex to count no. of alphabets in a scalar variable

how to construct regex to count no. of alphabets in a scalar variable

am 13.04.2008 06:35:01 von sunnyIsland

------=_NextPart_000_0009_01C89D62.CAAB5100
Content-Type: text/plain;
charset="gb2312"
Content-Transfer-Encoding: quoted-printable

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.
Thanks

{
my $string =3D 'hello there are many a aaa aaaa here.... a abbb =
baaaabba a please count me if you can.';
$count_a =3D 0;

@string =3D split //,$string;
foreach (@string){
$count_a ++ if /a/;
}
print "There are $count_a a's in \$string\n";
}
------=_NextPart_000_0009_01C89D62.CAAB5100--

Re: how to construct regex to count no. of alphabets in a scalar variable

am 13.04.2008 06:40:04 von chas.owens

On Sun, Apr 13, 2008 at 12:35 AM, 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: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: how to construct regex to count no. of alphabets in a scalar variable

am 13.04.2008 10:03:09 von sunnyIsland

> 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"
To:
Cc:
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, 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: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: how to construct regex to count no. of alphabets in a scalar variable

am 13.04.2008 15:13:29 von chas.owens

On Sun, Apr 13, 2008 at 4:03 AM, wrote:
>
> > 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.
snip

The match operator with option g returns different things based on its context:

#!/usr/bin/perl

use strict;
use warnings;

my $scalar = "aaaba" =~ /a/g;
my @list = "aaaba" =~ /a/g;
my $list_then_scalar = () = "aaaba" =~ /a/g;

print "scalar: $scalar list: @list list then scalar: $list_then_scalar\n";

In scalar context it returns true if it matches at all and in list
context it returns a list of matches. Now, normally a list in scalar
context returns the last element of the list, but if you evaluate the
list in list context and then in scalar context you instead get the
number of items in the list (similar to how an array acts in scalar
context).

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: how to construct regex to count no. of alphabets in a scalarvariable

am 13.04.2008 18:05:48 von krahnj

Chas. Owens wrote:
> On Sun, Apr 13, 2008 at 4:03 AM, wrote:
>>> 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.
> snip
>
> The match operator with option g returns different things based on its context:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $scalar = "aaaba" =~ /a/g;
> my @list = "aaaba" =~ /a/g;
> my $list_then_scalar = () = "aaaba" =~ /a/g;
>
> print "scalar: $scalar list: @list list then scalar: $list_then_scalar\n";
>
> In scalar context it returns true if it matches at all and in list
> context it returns a list of matches. Now, normally a list in scalar
> context returns the last element of the list,

That is the comma operator that does that. There are no commas in ().


> but if you evaluate the
> list in list context and then in scalar context you instead get the
> number of items in the list (similar to how an array acts in scalar
> context).


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: how to construct regex to count no. of alphabets in a scalar variable

am 13.04.2008 18:22:00 von chas.owens

On Sun, Apr 13, 2008 at 12:05 PM, John W. Krahn wrote:
snip
> > In scalar context it returns true if it matches at all and in list
> > context it returns a list of matches. Now, normally a list in scalar
> > context returns the last element of the list,
> >
>
> That is the comma operator that does that. There are no commas in ().
snip

I think you are splitting hairs (a list is a series of zero or more
items separated by commas). You can see that the following function
returns a list (the comma operators have already executed to build the
list), but when it is evaluated in a scalar context it returns the
last item and when evaluated in list then scalar contexts it is the
number of items in the list. Also note that when evaluated in list
then list context it produces what you would naively think it should
(i.e. an empty list).

#!/usr/bin/perl

use strict;
use warnings;

sub list { return "a", "b", "c" }

my $scalar = list();
my @list = list();
my $list_then_scalar = () = list();
my @list_then_list = () = list();

print
"scalar $scalar\n",
"list @list\n",
"list then scalar $list_then_scalar\n",
"list then list @list_then_list\n";

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

behavior regex code to count no. of alphabets in a scalar variable?

am 14.04.2008 07:06:08 von sanket vaidya

Go through the below codes.

use strict;
use warnings;

my $scalar = "aaaba" =~ /a/g;
my @list = "aaaba" =~ /a/g;
my $list_then_scalar =()= "aaaba" =~ /a/g;
print "scalar: $scalar\nlist: @list\nlist then scalar: $list_then_scalar\n";

$scalar="aaaba";
my $count = $scalar =~ tr/a//;
print "\n\nCount=$count\nScalar = $scalar";


Output:

scalar: 1
list: a a a a
list then scalar: 4


Count=4
Scalar = aaaba


Now the same code without initializing $scalar second time.

use strict;
use warnings;

my $scalar = "aaaba" =~ /a/g;
my @list = "aaaba" =~ /a/g;
my $list_then_scalar =()= "aaaba" =~ /a/g;
print "scalar: $scalar\nlist: @list\nlist then scalar: $list_then_scalar\n";

my $count = $scalar =~ tr/a//;
print "\n\nCount=$count\nScalar = $scalar";

Output:

scalar: 1
list: a a a a
list then scalar: 4


Count=0
Scalar = 1


Please explain this behavior i.e. why the values of count & scalar are 0 & 1
respectively?



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: behavior regex code to count no. of alphabets in a scalar variable?

am 14.04.2008 07:32:21 von chas.owens

On Mon, Apr 14, 2008 at 1:06 AM, sanket vaidya wrote:
snip
> my $scalar = "aaaba" =~ /a/g;
snip
> my $count = $scalar =~ tr/a//;
snip
> Count=0
> Scalar = 1
>
> Please explain this behavior i.e. why the values of count & scalar are 0 & 1
> respectively?
snip

$scalar holds the result of "aaaba" =~ /a/g in scalar context, which is 1
The string "1" has no "a"s in it so $count is 0

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/