in reply to Efficiently Extracting a Range of Lines (was: Range This)

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

Replies are listed 'Best First'.
Re: Re: Range This
by tachyon (Chancellor) on Jul 11, 2001 at 01:27 UTC

    As requested by /msg here is a comparison of the three methods so far using Benchmark. Particle's method is only 50% slower than a regex so it kicks a$$ too at more than three times faster than the range method. TIMTOWDI. For those who would like to learn a really easy cut'n'paste way to use Benchmark (like here) check out Benchmark made easy

    C:\>perl test.pl Benchmark: timing 100000 iterations of Mine, Particle, Yours... Mine: 5 wallclock secs ( 4.06 usr + 0.00 sys = 4.06 CPU) @ 24 +630.54/s ( n=100000) Particle: 8 wallclock secs ( 7.20 usr + 0.00 sys = 7.20 CPU) @ 13 +888.89/s ( n=100000) Yours: 25 wallclock secs (24.67 usr + 0.00 sys = 24.67 CPU) @ 40 +53.51/s (n =100000) C:\>