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

Discussion in 'Perl Misc' started by Pete Sammet, Nov 19, 2007.

  1. Pete Sammet

    Pete Sammet Guest

    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
     
    Pete Sammet, Nov 19, 2007
    #1
    1. Advertising

  2. (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
     
    Peter Makholm, Nov 19, 2007
    #2
    1. Advertising

  3. 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
     
    Gunnar Hjalmarsson, Nov 19, 2007
    #3
  4. 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
     
    Jürgen Exner, Nov 19, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    765
    Malcolm
    Oct 2, 2005
  2. nick048

    How to count occurencies in a Structure

    nick048, Apr 7, 2007, in forum: C Programming
    Replies:
    9
    Views:
    347
    Barry Schwarz
    Apr 8, 2007
  3. walterbyrd
    Replies:
    4
    Views:
    214
    Asun Friere
    May 17, 2007
  4. Mack

    Count number of bits set in a number

    Mack, Sep 27, 2007, in forum: C Programming
    Replies:
    12
    Views:
    818
    Mark Bluemel
    Sep 28, 2007
  5. Larry
    Replies:
    5
    Views:
    785
    Bernard
    Mar 13, 2008
Loading...

Share This Page