Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I do like Dave's answer. But there is one thing that he didn't tell us which is that $count has to be initialized before calling this statement when using strict and warnings. $limit could have been a constant and I think that is the same.
#!/usr/bin/perl -w use strict; my $str='dog dog horse dog cow dog pig dog'; my $count =0; # won't work unless $count intitialized to 0 # simple declaration of "my $count; or a "my" # variable within in this type of complex perl # statement doesn't work under strict and warnings. # Or at least I've not been able get this # kind of stuff to work before. # But given my caveats, this does work! # whether is is better or not is left to # the readers.. my $limit =3; $count++ while $count < $limit && $str =~ m/dog/g; print "$count dogs were counted\n"; my $str2 = "horse dog cow"; $count = 0; #needed for initialization $count++ while $count < $limit && $str2 =~ m/dog/g; print "$count dogs were counted\n"; __END__ 3 dogs were counted 1 dogs were counted
Now one good thing about Dave's code is that it will stop after a certain number of matches - or at least I think that is what is going to happen (the match global gizmo can be modulated and monitored as it progresses). Whether or not that really matters depends upon the length of the string and the number of "dogs". I would claim that with less than 20 animals, it doesn't matter at all. Once the string gets much bigger than than that, well, it could matter. Software is art with science as a base, but there are exceptions to every "rule". What is the best in this application is just not known. That's why I show some examples of how to use Dave's code. Its a good idea and worthy of consideration, especially if the data set is very large.

In reply to Re^2: Limiting number of regex matches by Marshall
in thread Limiting number of regex matches by eversuhoshin

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (10)
    As of 2015-12-02 06:17 GMT
    Find Nodes?
      Voting Booth?

      My keyboard shows this many letters:

      Results (35 votes), past polls