But for the original sample of data, it is hideous.
#!/usr/bin/perl -w $|++; use strict; use Benchmark; sub arg2 { my($str,$lvl)= @_; my $str1 = $str; return $str if !$lvl; # skip up to and including nth ( paren, then strip n ) parens from +end $str =~ /([^(]*\(){$lvl}(.+)(\)){$lvl}/; return $2 unless $lvl == -1; $str =~ /\(([^\(\)]+)\)+/; return $1; } sub getpropstr { local $_ = shift; my $level = shift || 0; if ( $level == -1 ) { /([^()]+)\)+$/; return $1 } while ( $level-- > 0) { chop; s/^[^(]+\(//; } return $_; } my @data = <DATA>; chomp @data; if(0) { for (@data) { print "$_\n"; print getpropstr( split ), "\n" ; print get_proparg( split ), "\n" ; print prop3( split ), "\n" ; # print peeln( split ), "\n" ; print arg2( split ), "\n" ; } } my $count = 15000; timethese ( $count, { 'mine-elegant' => sub { for (@data) { prop3( split ); } }, 'mine-less-elegant' => sub { for (@data) { getpropstr( split +); } }, 'original' => sub { for (@data) { get_proparg( split ); } }, # 'other' => sub { for (@data) { peeln( split ); } }, 'yours-arg2' => sub { for (@data) { peeln( split ); } }, } ); sub peeln { local $_ = shift; my $n = shift || 0; my($c) = (tr[(][]); # warn 'Unbalanced parens' unless $o == $c; $n = $c if $n == -1; m[^ (?: .*? \( .*?){$n} (.+) (?: .*? \) .*?){$n} $]x; $1; } sub get_proparg { my $propstr = shift; # get the property string my $level = shift || 0; # get the level we want to extract my $back = $propstr; # Initialize Return value (for case leve +l=0) my $cnt; # initialize counter if($level == -1) { # special case, get the innermost argume +nt $propstr =~ /\(([^\(\)]+)\)+/; $propstr = $1; } else { # get whatever argument $level indicates for($cnt = 0;$cnt<$level; $cnt++) { $propstr =~ /\((.+)\)/; $propstr = $1; } } return $propstr; } sub prop3 { my $str = shift; my $level = shift || 0; return $str unless $level; my @str = split(/[()]/, $str); splice @str, 0, $level; my $out = join('(', @str); $out .= ')' x ( @str - 1 ); return $out; } __END__ hello(what(is(this(all(about))))) -1 hello(what(is(this(all(about))))) 0 hello(what(is(this(all(about))))) 1 hello(what(is(this(all(about))))) 2 hello(what(is(this(all(about))))) 3 hello(what(is(this(all(about))))) 4 hello(what(is(this(all(about))))) 5
Results:
Benchmark: timing 15000 iterations of mine-elegant, mine-less-elegant, + original, yours-arg2... mine-elegant: 7 wallclock secs ( 5.19 usr + 0.00 sys = 5.19 CPU) @ +2890.17/s (n=15000) mine-less-elegant: 4 wallclock secs ( 3.19 usr + 0.01 sys = 3.20 CP +U) @ 4687.50/s (n=15000) original: 6 wallclock secs ( 5.06 usr + 0.02 sys = 5.08 CPU) @ 29 +52.76/s (n=15000) yours-arg2: 32 wallclock secs (25.06 usr + 0.08 sys = 25.14 CPU) @ 59 +6.66/s (n=15000)
The lesson I'm taking away from this: simple regexes can be very fast, but time increasses rapidly with regex complexity.

--Bob Niederman, http://bob-n.com

In reply to Re: Re: Peeling the Peelings by bobn
in thread Peeling the Peelings by PetaMem

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.