How to count the number of "a" occurencies in a string ?

P

Pete Sammet

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
 
P

Peter Makholm

$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
 
G

Gunnar Hjalmarsson

Pete said:
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.
 
J

Jürgen Exner

Pete said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top