1) forget split ~ why split, parse, then join?

2) forget m// ~ when you have index

3) generalize for more tags

try this~

sub find_between_tags($$$) { # pass me the string to parse, the start tag, and the end tag my $string_to_parse = shift; my $start_tag = shift; my $start_pos = index $stuff, $start_tag; # get string position of s +tart tag return unless $start_pos > -1; # return unless start tag +found my $stop_tag = shift; my $stop_pos = index $stuff, $stop_tag; # get string position of e +nd tag $stop_pos > -1 ? # end tag found, return su +bstring return substr $stuff, $start_pos, # return substring $stop_pos - $start_pos + length $stop_tag : # including matching + tags return substr $stuff, $start_pos, -1; # return substring to end } print find_between_tags($stuff, "START", "STOP");

Update:

thanks tachyon, for teaching me Benchmark!

here's the lowdown on original method vs. mine:

Benchmark: timing 100000 iterations of mine, original... mine: 4 wallclock secs ( 3.28 usr + 0.00 sys = 3.28 CPU) @ 30 +534.35/s (n=100000) original: 14 wallclock secs (13.12 usr + 0.00 sys = 13.12 CPU) @ 76 +22.53/s (n=100000)

~Particle


In reply to Re: Range This by particle
in thread Efficiently Extracting a Range of Lines (was: Range This) by skazat

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.