how to get # of occurances of a character in a given string

how to get # of occurances of a character in a given string

am 16.09.2007 13:22:57 von vijay

$str = "vijaya";

if my character is 'a' then it should get the number 2

Re: how to get # of occurances of a character in a given string

am 16.09.2007 14:15:14 von Martien Verbruggen

On Sun, 16 Sep 2007 11:22:57 -0000,
vijay@iavian.com wrote:
> $str = "vijaya";
>
> if my character is 'a' then it should get the number 2

And the actual question is:

> how to get # of occurances of a character in a given string

which I has to get from the subject line.

One possible answer can be found in the perlop documentation, under the
tr/// operator.

my $count = $str =~ tr/a//;

Martien
--
|
Martien Verbruggen | We are born naked, wet and hungry. Then
| things get worse.
|

Re: how to get # of occurances of a character in a given string

am 16.09.2007 14:19:59 von Ben Morrow

Quoth "vijay@iavian.com" :
> $str = "vijaya";
>
> if my character is 'a' then it should get the number 2

perldoc -q count

Ben

Re: how to get # of occurances of a character in a given string

am 16.09.2007 14:20:52 von Dummy

vijay@iavian.com wrote:
> $str = "vijaya";
>
> if my character is 'a' then it should get the number 2

$ perl -le'
my $str = "vijaya";
my $count = $str =~ tr/a//;
print $count;
'
2


perldoc -q count


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