in reply to Calculations in Regexp

Why not just $flag_count++ if ($data=~/<BEGIN_FLAG>/); ?

if you expect more than one match in a single string, check out the 'g' modifier in perlop and what it does in scalar context

You could also use a substitution regex with ge modifier, but that sounds a bit like a hack, since you do no substitution

PS: Is this homework, the problem seems somewhat artifical?

Replies are listed 'Best First'.
Re^2: Calculations in Regexp
by sumeetgrover (Monk) on Jan 20, 2011 at 15:04 UTC

    Thanks for your answers!

    I am working on a large text file and putting markers around data and was trying to find out efficient ways.

    I have finally decided to use:
     
    while($data=~<BEGIN_FLAG>) {$count++;}
     
    Thank you!

      Don't forget the g modifier or this will be an endless loop. And don't forget the slashes or this won't even be a regex

      If that is what you call “efficiency,” you are barking up the wrong tree for the wrong reasons.   No matter how “large” your text file may be, the CPU will spend the vast majority of its wall-time waiting for I/O.   It will spend an insignificant amount of time incrementing a variable, or even schlepping text around in memory (as long as virtual-storage paging is not “sneaking-in a significant amount of hidden I/O”).

      What is a valid engineering concern, however, is just how easy it will continue to be to understand and to maintain your code in the future ... long after you have been smooshed (oops... hate it when that happens) by that most unfortunate bread truck.   Will your “clever trick” seem quite so clever when it turns into a bug?   Food for thought, and offered in that constructive spirit.