in reply to Efficiently Extracting a Range of Lines (was: Range This)
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");
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 |