in reply to Re^3: Refactoring challenge.
in thread Refactoring challenge.
I still think your code is doing a lot of manual pattern matching which could be better done by the regex engine.
By that, I assume you mean the indexBal() routine.
If you can encapsulate this into a more efficient, or more simple routine by using the regex engine, please do, it would help me a lot. I tried and failed.
sub indexBal { my( $string, $lookFor, $limit ) = @_; my $nesting = 0; for( my( $position, $char ) = ( 0, substr $string, 0, 1 ); $position < min( length( $string ) , $limit ); $char = substr $string, ++$position, 1 ) { $nesting++ if $char =~ m/[\[\{]/; $nesting-- if $char =~ m/[\]\}]/; die 'Unbalanced' if $nesting < 0; return $position if $char eq $lookFor and $nesting == 0 } return; } my $s = '{ [ { [ { x }, { y } ], [ z ] } ] } { [ { '; my $pos = indexBal $s, '}', length( $s ); print substr $s, 0, $pos+1; { [ { [ { x }, { y } ], [ z ] } ] }
Note that:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Refactoring challenge.
by tphyahoo (Vicar) on Mar 08, 2005 at 09:26 UTC | |
by BrowserUk (Patriarch) on Mar 08, 2005 at 12:57 UTC |