Well, I generally prefer to break things into more verbose code to make life easier 2 years down the road (or next week) when I have to change something.

Too, experience said my approach should be faster since more but easier (for Perl) regexes are typically faster than one complex one.

Just out of curiosity I ran a test and Benchmark confirmed my suspicion.

I set up the test with each sub populating $string from a global, processing it and returning the results. With 10,000,000 iterations it's around 8.75 seconds for the one liner, and around 4.75 for my multi-line example on my machine.

I fiddled the code a bit to see if minor variations made any difference with little change in the times in either approach.

So, a little less than twice as fast in execution time...

Not a big deal at all with any reasonable number of string matches, but a little here, a little there...

Anyway, the OP was presented with several options. Life is good.

\s


In reply to Re^5: substr question by stevenmay
in thread substr question by Anonymous Monk

Title:
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, details, 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, summary, 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.