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

Hello Monks

I have string such that -

“The Expo is not the place to promote trade, this is about culture,” however ,

I want to count the no. of double quote (which is 2 in this case). Is split function is only option? Can this not be handled by reg expression?

Replies are listed 'Best First'.
Re: count special character
by moritz (Cardinal) on Aug 18, 2010 at 08:54 UTC
    On the command line, type
    perldoc -q count

    And amazingly, it will answer your question. See also: perlfaq4.

    Perl 6 - links to (nearly) everything that is Perl 6.
      I think i have got the answer
      $count = ($string =~ tr/\"//); print "There are $count [\" ] characters in the string";
Re: count special character
by Corion (Patriarch) on Aug 18, 2010 at 08:31 UTC

    What happened if you tried?

    You could extract and count the number of "special" characters. Or you could (extract and) count the number of "normal" characters, and subtract that from the length of the string. See perlop on how to match, and perlre on how to create your regular expression.

    You could also eliminate all "normal" characters and count what is left over. See perlop on the tr/// operator.

Re: count special character
by changma_ha (Sexton) on Aug 18, 2010 at 10:44 UTC

    I hope this will help u .

    #! /usr/bin/perl use warnings; use strict; my $string = "The Expo is not the place to promote trade, this is abou +t culture,"; my $count = ($string =~ tr/,/ /); print $count;