in reply to Counting text strings

You could change this code:
while (<FILE>) { if (/foo/) { $count++; print "$count so far.\n"; } }
to something like this:
while (<FILE>) { while (/foo/g) { $count++; print "$count so far.\n"; } }
update: or something like this:
while (<FILE>) { $count++ while /foo/g; } print $count;

-enlil

Replies are listed 'Best First'.
Re: Re: Counting text strings
by l2kashe (Deacon) on Mar 04, 2003 at 06:30 UTC
    Or something slightly different yet the same
    while ( <FILE> ) { $count += $_ =~ s/foo/foo/g; print "$count so far\n"; }
    If you do $count = $_ =~ s/foo/foo/g; $count will have how many replacements happened on that line. Instead of using a temp variable to hold it, I simply += it as we go.. I should probably benchmark it to see how it compares, but its late.

    /* And the Creator, against his better judgement, wrote man.c */
Re: Re: Counting text strings
by vector40 (Initiate) on Mar 04, 2003 at 06:09 UTC

    Hey, wow -- cool stuff, works perfectly. Great response time :)

    Can you explain why this made it work? I think I get why the global bit was necessary for the regex (otherwise, it wouldn't check the whole file); however, why did using while instead of if make a difference?

      Another way you could do this:
      while (<FILE>) { $count += () = m/foo/g } print $count;
      But back to the way this:
      while (<FILE>) { while (/foo/g) { $count++; print "$count so far.\n"; } }
      works is that the inner while will continue to evaluate $_ =~ /foo/g, (which is the lengthier version of: /foo/g ), anyhow it will continue to evaluate it until it has no more matches. So it will find the first foo then stop update the count, and then return to the search beginning where it left off, and so forth until it finds no more matches, but adding one to count each time it does find one. (I hope I am not confusing you further).

      The if does not work because it only cares if the expression in the parens returns a true or false value. Strings with foo are true, add one to your count and on to the next line. (strings without foo are false of course). You could do something like the following:

      my $count; while (<FILE>) { if ( $count += s/foo/foo/g ) { print "$count so far\n" ) }
      Here the s/foo/foo/g returns the number of times it replaces foo with itself adds it to the count, and the number it returns is also the number the if checks for truth ( so a line like "foo foo foo" would be evaluated as if (3), as opposed to whether or not the addition to $count was successful.)

      anyhow I hope I was some help, it is late and i tend to ramble anyhow.

      oh yeah, ++ for asking for an explanation instead of just taking code without understanding it (and having to explain it often times makes me wonder if I understand it myself).

      -enlil

        Actually, that was insightful. I mean, it's late here too, and maybe that's why but that was one of the most cogent explanation's I've ever heard :)

        Makes sense now; thanks all.

      What he did is perform another loop, with each iteration happening everytime the regex matched in the string...
      Ala
      @test = ('foobarfoo', 'blahbarblah', 'bazbarbarbarbaz'); while (@test) { # # $_ has foobarfoo or blahbarblah or bazbarbarbarbaz # while(/bar/g) { # # matched bar once in foobarfoo inc by 1 # matched bar once in blahbarblah inc by 1 # matched bar 3 times in bazbarbarbarbaz inc by 1 3 times # $count++; print "$count so far\n"; } }
      Edit: Sorry for replicating information, Enlil and myself were probably typing at the same time..


      /* And the Creator, against his better judgement, wrote man.c */