[Israel.pm] count chars

Yuval Kogman nothingmuch at woobling.org
Mon Sep 3 07:47:30 PDT 2007


Note that tr/// will work for unicode strings, but for Inline::C to
do that more work will be necessary.

On Mon, Sep 03, 2007 at 17:42:12 +0300, Pinkhas Nisanov wrote:
> Hi,
> 
> On 9/3/07, Amir E. Aharoni <amir.aharoni at gmail.com> wrote:
> > What is the best way to count the number of times that a character
> > appears in a string?
> 
> if you need good performance, use "Inline::C".
> e.g.:
> 
> 
> use Inline C;
> 
> print "Char in Str: ". chCnt( 'a', 'ajhajhhjajhjajh' ). "\n";
> ### you get:
> ### Char in Str: 4
> 
> __DATA__
> __C__
> 
> int chCnt ( char lookChar, char *str  ) {
>   char c;
>   int i = 0;
>   int currCnt = 0;
> 
>   while ( c = str[i++] )
>     {
>       if ( c == lookChar )
> 	{
> 	  ++currCnt;
> 	}
>     }
> 
>   return currCnt;
> }
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl

-- 
  Yuval Kogman <nothingmuch at woobling.org>
http://nothingmuch.woobling.org  0xEBD27418




More information about the Perl mailing list