[Israel.pm] count chars

Gaal Yahas gaal at forum2.org
Tue Sep 4 10:27:30 PDT 2007


On 9/3/07, Amir E. Aharoni <amir.aharoni at gmail.com> wrote:
> What is the best way to count the number of times that a character
> appears in a string?
>
> Currently i do this:
>
> ----
> my $string = "abracadabra";
> my @matches = ($string =~ /a/g);
> print scalar @matches; # 5
> print "@matches"; # a a a a a
> ----
>
> Is there a way to do it without the intermediate array?

Yes.

my $count = () = $string =~ /a/g;

-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list