How to count the number of "a" occurencies in a string ?
How to count the number of "a" occurencies in a string ?
am 19.11.2007 09:38:07 von petesammet
Sorry for this newbie question:
Is there a function in Perl for counting the number of e.g. "a" in a String ?
For example:
$num = countchars("abbbabbbaba", 'a');
should yield 4. Unfortunately the function "countchars" does not exist.
is there another one-liner function ?
Another realted but more advanced question:
Is there a counter function for counting INDEPENDENT "a" in a String?
In this case concatenated "a" should be counted as 1. e.g.
$num = countcharschains("aammmammaaaaam", 'a');
Should yield 3
Pete
Re: How to count the number of "a" occurencies in a string ?
am 19.11.2007 09:58:05 von Peter Makholm
petesammet@mail.org (Pete Sammet) writes:
> $num = countchars("abbbabbbaba", 'a');
>
> should yield 4. Unfortunately the function "countchars" does not exist.
Yes it does, it is called tr///
makholm@makholm:/tmp$ perl -le '$foo = "abbbabbbaba"; print ($foo =~ y/a//); print $foo'
4
abbbabbbaba
makholm@makholm:/tmp$
> Is there a counter function for counting INDEPENDENT "a" in a String?
> In this case concatenated "a" should be counted as 1. e.g.
>
> $num = countcharschains("aammmammaaaaam", 'a');
>
> Should yield 3
scalar( ()= "aammmammaaaaam" =~ m/a+/g );
//Makholm
Re: How to count the number of "a" occurencies in a string ?
am 19.11.2007 09:58:09 von Gunnar Hjalmarsson
Pete Sammet wrote:
> Is there a function in Perl for counting the number of e.g. "a" in a String ?
>
> For example:
>
> $num = countchars("abbbabbbaba", 'a');
>
> should yield 4. Unfortunately the function "countchars" does not exist.
>
> is there another one-liner function ?
No, but there is an applicable FAQ entry.
perldoc -q count.+string
> Another realted but more advanced question:
> Is there a counter function for counting INDEPENDENT "a" in a String?
> In this case concatenated "a" should be counted as 1. e.g.
>
> $num = countcharschains("aammmammaaaaam", 'a');
>
> Should yield 3
Same FAQ.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: How to count the number of "a" occurencies in a string ?
am 19.11.2007 10:08:19 von jurgenex
Pete Sammet wrote:
> Is there a function in Perl for counting the number of e.g. "a" in a
> String ?
> $num = countchars("abbbabbbaba", 'a');
> should yield 4. Unfortunately the function "countchars" does not
> exist.
perldoc -f tr
tr/SEARCHLIST/REPLACEMENTLIST/cds
Transliterates all occurrences of the characters found in the
search list with the corresponding character in the replacement
list. It RETURNS THE NUMBER OF CHARACTERS REPLACED OR DELETED.
my $foo = "abbbabbbaba";
print ($foo =~ tr/a/a/);
> Another realted but more advanced question:
> Is there a counter function for counting INDEPENDENT "a" in a String?
> In this case concatenated "a" should be counted as 1. e.g.
>
> $num = countcharschains("aammmammaaaaam", 'a');
>
> Should yield 3
perldoc -f s:
s/PATTERN/REPLACEMENT/egimosx
Searches a string for a pattern, and if found, replaces that
pattern with the replacement text AND RETURNS THE NUMBER OF
SUBSTITUTIONS MADE.
jue