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, ], ], }

Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

In reply to Re: Refactoring challenge. by BrowserUk
in thread Refactoring challenge. by BrowserUk

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.