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 | |
by gjb (Vicar) on Jul 02, 2003 at 19:46 UTC |