SPUG: To sum it up... counting the number of occurences...

Jonathan Gardner gardner at sounddomain.com
Mon Dec 18 16:52:36 CST 2000

To count the number of occurences of a rgex match in a string, I got the
following suggestions:

$count = $string =~ tr/a//; # From several of you...

This works only if you are looking for individual characters. That isn't
what I intended, but it was a very good solution to the example I posed. It
is very fast, too. Trouble is that I never even think of using tr even when
I should. It sits in the corner of my mind called "Perl Trivia" along with
all the other stuff you can do but won't remember until someone else points
out you should've.

$count =0;
$string =~ s/a/ ++$count , 'a' /eg; # Thanks to Jason Lampert

This one is a little less obvious, but if you are familiar with the ","
operator, it makes perfect sense. Each time the search comes up with a
match, it executes the second half, which increments the count and returns
the sub. The problem is you have to replace it if you want the string intact
afterwards. I don't know what the overhead for this one is time wise
compared to the following.

There is another version using the matching operator:

$count = () = $string =~ /a/g; # Thanks to Daniel Chetlin

This one is a bit cryptic, but it gets the point across. How in the WORLD
did you know about doing

$SizeOfTheArrayReturnedByTheFunction = () =

is a mystery to me... but now I know. The main point from this is that in
array context, m//g returns an array of all the matches.

There is a similar one:

$count = 0;
$count ++ while ($string =~ /a/g); # Thanks to Joel Grow

That matches em all, puts it in an array and iterates over the array
incrementing count each time. I learn more about "while" and "for/foreach"
every day in perl. Those two functions have endless possibilities...

The next one comes from Jason Lamport as well, it having to use the variable

$/ = 'a';
$count = -1;
while (<>) { ++$count; }

This is pretty cool, but you can't use regexes. $/ is one of those variables
that I read about, forgot, seen it used, forgot, and seen it used again, but
keep forgetting about. I suspect that if you have large files you want to
count, this is the way to go.

There's more than one way to do it, as the saying goes... Thanks for all the

Jonathan M. Gardner
CarDomain Networks
(425)820-2244 x23
gardner at sounddomain.com

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/

More information about the spug-list mailing list