in reply to Peeling the Peelings

The following should do what you want if the input is in the format you specify, i.e. nested function with single arguments.

my $example1 = 'this(is(a(test)))'; foreach (0..4) { print "level $_: '", peel($example1, $_), "'\n"; } print "level -1: '", peel($example1, -1), "'\n"; sub peel { my ($str, $level) = @_; if ($level < 0) { $str =~ /([^\(\)]+)\)+\Z/; return $1; } my $opening = '(?:[^\(]+\(){' . $level . '}'; my $closing = '\){' . $level . '}'; $str =~ /\A$opening(.+?)$closing\Z/; return $1; }

Hope this helps, -gjb-

Replies are listed 'Best First'.
Re: Re: Peeling the Peelings
by waswas-fng (Curate) on Jul 01, 2003 at 21:01 UTC
    Rate Peel Get_Proparg Peel 1359/s -- -76% Get_Proparg 5780/s 325% --
    Looks like thats a wee bit slower.

    -Waswas

      It's easy to optimize a bit, although it's still 20% slower than Aristotle's version :-(

      my %cache = ('-1' => qr/([^\(\)]+)\)+\Z/); sub peel { my $str = shift; my $level = shift || 0; if (!exists $cache{$level}) { my $opening = '[^\(]+\(' x $level; my $closing = '\)' x $level; $cache{$level} = qr/\A$opening(.+?)$closing\Z/; } $str =~ $cache{$level}; return $1; }
      The benchmark results are:
      Rate Fatvamp gjb bobn Aristotle Fatvamp 21155/s -- -14% -17% -28% gjb 24631/s 16% -- -3% -16% bobn 25523/s 21% 4% -- -13% Aristotle 29497/s 39% 20% 16% --

      Best regards, -gjb-