slloyd has asked for the wisdom of the Perl Monks concerning the following question:

I have the following string: $str="abc%def%.%";
How is the quickest way to return the number of times '%' is in the string. Here is what I have come up with, but is there an easier way?
use strict; my $str='abc%def%.%'; #Get the count by removing all but the percent sign my $pstr=$str; $pstr=~s/[^\%]//sg; my $pcount=length($pstr); print "str: $str\n"; print "pstr: $pstr\n"; print "pcount: $pcount\n";

-------------------------------
by me
http://www.basgetti.com
http://www.kidlins.com

Replies are listed 'Best First'.
Re: Counting how many times a character is in a string.
by Aristotle (Chancellor) on Nov 18, 2005 at 16:15 UTC
    my $pcount = $str =~ tr/%//;

    See tr.

    Makeshifts last the longest.

Re: Counting how many times a character is in a string.
by prasadbabu (Prior) on Nov 18, 2005 at 16:16 UTC

    Simply do the following

    my $count = $str =~ tr/%//; print "pcount: $count\n";

    Prasad

Re: Counting how many times a character is in a string.
by Samy_rio (Vicar) on Nov 18, 2005 at 16:20 UTC

    Hi, try this link, Any other options for total count

    In this link, you put character instead of tags.

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: Counting how many times a character is in a string.
by marto (Cardinal) on Nov 18, 2005 at 16:20 UTC
    Hi slloyd,

    Have a look tr "Transliterates all occurrences of the characters found in the search list with the corresponding character in the replacement list." (from perlop).
    For example:
    $string="abc%def%.%"; $count = ($string =~ tr/%//); print "There are $count %s in the string";
    Hope this helps.

    Martin
Re: Counting how many times a character is in a string.
by davidrw (Prior) on Nov 18, 2005 at 17:06 UTC
    as mention several times already, tr is probably best for single characters. Another solution, which is scalable to multi-charater delimiters, is to use split:
    use strict; use warnings; my $str='abc%def%.%'; printf "pcount: %d\n", split(/%/, $str.' ')-1;
    (the .' ' is for the case of a trailing %, where split throws out the last empty element)
Re: Counting how many times a character is in a string.
by thundergnat (Deacon) on Nov 18, 2005 at 17:53 UTC

    Update: Nevermind, I was completely wrong here. Thanks to Aristotle for setting me straight. Apparently, I just breezed right over that section of the docs.

    As has been mentioned several times, tr is the fastest and easiest way to get a count a particular character in a string. All of the examples up to this point have been destructive though. If you want to get a count without modifying the string, simply transliterate the character to itself.

    my $str='abc%def%.%'; my $count = $str =~ tr/%/%/; print "\"$str\" has $count % in it.\n";

      All of the examples up to this point have been destructive though.

      Wrong.

      Quoting perlop on tr:

      If the /d modifier is used, the REPLACEMENTLIST is always interpreted exactly as specified. Otherwise, if the REPLACEMENTLIST is shorter than the SEARCHLIST, the final character is replicated till it is long enough. If the REPLACEMENTLIST is empty, the SEARCHLIST is replicated. This latter is useful for counting characters in a class or for squashing character sequences in a class.

      (Underline mine.)

      Makeshifts last the longest.

        If the REPLACEMENTLIST is empty, the SEARCHLIST is replicated. This latter is useful for counting characters in a class
        Furthermore, this used to effectively replace characters by themselves, causing it to fail on read-only values, but that has been fixed. Now tr/%// merely counts.