http://www.perlmonks.org?node_id=995643


in reply to Re^2: Limiting number of regex matches
in thread Limiting number of regex matches

To be accurate, it does work with or without initializing $count to zero. But on the first iteration, the undefined $count will be treated as though it were zero, and a warning will be generated letting the programmer know that he probably should initialize $count to zero explicitly before using it in the context of a numeric comparison (assuming warnings are enabled, as they probably ought to be).

As for why the example doesn't explicitly use strict, first it seemed that the OP already had a handle on how to declare lexical variables, and second, "Well, because it's a four-line one-line example program I concocted as an example in my Usenet PerlMonks article---duh!"

(I hope the intended humor isn't lost in this post, your point is valid.)

Oh, and you're correct; the process stops as soon as the $limitth match occurs, which is a good approach since it stops extra work from happening. Think of it as the difference between List::Util's first function, and the core's grep.


Dave

Replies are listed 'Best First'.
Re^4: Limiting number of regex matches
by Marshall (Canon) on Sep 25, 2012 at 22:56 UTC
    I would personally consider a "warning" as an error even if it is the first one.

    I don't see any really significant disagreement here. I ran your code and it it works. And you ran my code and it works.

    Ok, there are different assumptions as to data set size, etc. But at the end of the day, the OP got two great ideas and he/she can try them both and see what happens in the particular application.

    I claim mutual success! We both did a good job.

      No disagreement whatsoever. I was just clarifying.

      Cheers!


      Dave