home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!gatech!concert!uvaarpa!mmdf
- From: Kevin Burton <noran!iowa!kburton@uunet.uu.net>
- Subject: Counting characters that match an arbitrary pattern.
- Message-ID: <1992Sep9.191438.25772@uvaarpa.Virginia.EDU>
- Sender: mmdf@uvaarpa.Virginia.EDU (Mail System)
- Reply-To: noran!iowa!kburton@uunet.uu.net
- Organization: The Internet
- Date: Wed, 9 Sep 1992 19:14:38 GMT
- Lines: 31
-
-
- I am sure this is a candidate for FAQ but I didn't see it in the "official"
- perl FAQ list so here it is.
-
- What is the most efficient subroutine that counts the number of characters
- that match a particular pattern. Currently I have:
-
- sub countchar
- {
- local($pattern,$string) = @_;
- local($count);
- local($_);
-
- $count = ($string =~ s/$pattern//g);
-
- return $count;
- }
-
-
- printf "Count = %d\n",&countchar('[A-Z]',"abcdEFGH");
-
- Is there a better, more efficient way ?
-
- Thanks.
-
- kburton@noran.com
-
-
-
-
-
-