home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5804 < prev    next >
Encoding:
Text File  |  1992-09-09  |  1.0 KB  |  43 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!gatech!concert!uvaarpa!mmdf
  3. From: Kevin Burton <noran!iowa!kburton@uunet.uu.net>
  4. Subject: Counting characters that match an arbitrary pattern.
  5. Message-ID: <1992Sep9.191438.25772@uvaarpa.Virginia.EDU>
  6. Sender: mmdf@uvaarpa.Virginia.EDU (Mail System)
  7. Reply-To: noran!iowa!kburton@uunet.uu.net
  8. Organization: The Internet
  9. Date: Wed, 9 Sep 1992 19:14:38 GMT
  10. Lines: 31
  11.  
  12.  
  13. I am sure this is a candidate for FAQ but I didn't see it in the "official"
  14. perl FAQ list so here it is.
  15.  
  16. What is the most efficient subroutine that counts the number of characters 
  17. that match a particular pattern. Currently I have:
  18.  
  19.        sub countchar
  20.        {
  21.                   local($pattern,$string) = @_;
  22.                   local($count);
  23.                   local($_);
  24.        
  25.                   $count = ($string =~ s/$pattern//g);
  26.               
  27.                   return $count;
  28.        }
  29.  
  30.  
  31.        printf "Count = %d\n",&countchar('[A-Z]',"abcdEFGH");
  32.  
  33. Is there a better, more efficient way ?
  34.  
  35. Thanks.
  36.  
  37. kburton@noran.com
  38.  
  39.  
  40.     
  41.  
  42.  
  43.