in reply to Refactoring challenge.
The final incarnation (unless you know better?), with grateful thanks to all who helped especially dragonchild, hv & Roy Johnson
#! perl -slw use strict; use List::Util qw[ min ]; $|=1; 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 1+index '[{', $char; $nesting-- if 1+index ']}', $char; die 'Unbalanced' if $nesting < 0; return $position if $char eq $lookFor and $nesting == 0 } return; } my $depth = 0; my $indent = ' '; my $string = ''; sub findChunk { my( $str, $width ) = @_; $str =~ m/([\Q[]{},\E])/g or return (); my( $pos, $first ) = ( $+[0], $1 ); return( $pos +1, 0 ) if $first eq ','; return( $pos +1, -1 ) if 1+index ']}', $first; if( my $pos2 = indexBal( $str, $first eq '[' ? ']' : '}', $width ) + ) { return( $pos2 +2, 0 ); } return( $pos , +1 ); } sub pp { my( $nextBit, $width, $EOS ) = @_; $string .= $nextBit; while( length( $string ) > $width or $EOS ) { my( $pos, $adjust ) = findChunk( $string, $width ); return unless defined $pos; $depth-- if $adjust < 0; print $indent x $depth, substr( $string, 0, $pos, '' ); $string =~ s[^\s*][]; $depth++ if $adjust > 0; } return; } ## Test harness only below here my $data = <DATA>; my $width = $ARGV[ 0 ]||100; $indent = $ARGV[ 1 ] if @ARGV == 2; while( length $data ) { my $p = 1+rindex( $data, ',', 1+rand( 100 ) )||length $data; my $sub = substr( $data, 0, $p, '' ); pp( $sub, $width, !length $data ); } __DATA__ { a => [ SCALAR(0x18bb45c), { a => b, c => d, e => f, g => h, }, [ 1, + 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => h, } +, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => + h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f +, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, +e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], ], b => [ S +ELFREF(018bb45c), { a => b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, + 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => h, }, [ 1, 2, + 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => h, }, [ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => h, + }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g + => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], ], c => [ SELFREF(01 +8bb45c), { a => b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, + 8, 9, 10, ], { a => b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, + 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => h, }, [ 1, 2, 3, + 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => h, }, [ 1, + 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => h, } +, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], ], d => [ SELFREF(018bb45c), +{ a => b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 +, ], { a => b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, ], { a => b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, + 7, 8, 9, 10, ], { a => b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, + 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => h, }, [ 1, 2, + 3, 4, 5, 6, 7, 8, 9, 10, ], ], e => [ SELFREF(018bb45c), { a => b, + c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a + => b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, +], { a => b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, ], { a => b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, + 8, 9, 10, ], { a => b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, + 6, 7, 8, 9, 10, ], ], f => [ SELFREF(018bb45c), { a => b, c => d, +e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c +=> d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => + b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], +{ a => b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 +, ], { a => b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, ], ], g => [ SELFREF(018bb45c), { a => b, c => d, e => f, g + => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e = +> f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => +d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, + c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a + => b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, +], ], h => [ SELFREF(018bb45c), { a => b, c => d, e => f, g => h, } +, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => + h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f +, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, +e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c +=> d, e => f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], ], i +=> [ SELFREF(018bb45c), { a => b, c => d, e => f, g => h, }, [ 1, 2, + 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => h, }, [ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => h, + }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g + => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e = +> f, g => h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], ], j => [ SELF +REF(018bb45c), { a => b, c => d, e => f, g => h, }, [ 1, 2, 3, 4, 5, + 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => h, }, [ 1, 2, 3, + 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => h, }, [ 1, + 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => h, } +, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], { a => b, c => d, e => f, g => + h, }, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], ], }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Refactoring challenge.
by Roy Johnson (Monsignor) on Mar 09, 2005 at 06:57 UTC | |
|
Re^2: Refactoring challenge.
by tphyahoo (Vicar) on Mar 08, 2005 at 17:14 UTC | |
by BrowserUk (Patriarch) on Mar 08, 2005 at 17:31 UTC |