It won't produce 'optimal' results, but for display purposes it will probably be 'good enough'. It also has the virtues of being simple and determanistic (fast).

Basically, sort the segment/length pairs ascending by segment, descending by length. Scan the sorted array backward putting the next value into the current group (line) if it doesn't overlap the previous entry. When you can fit no more, start a new group (line). Repeat until no more bits:

#! perl -slw use strict; use Data::Dump qw[ pp ]; ## Generate some data my $string = '1234567890' x 7; my @bits = map[ int rand length $string, 2 + int rand 10 ], 1 .. 20; ## Sort ascending offset/descending length @bits = sort{ $a->[ 0 ] <=> $b->[ 0 ] || $b->[ 1 ] <=> $a->[ 1 ] } @bits; ## build groups my @groups; ## Till done while( @bits ) { ## start a new group with the last bit push @groups, [ pop @bits ]; ## Scan the rest of the bits (backward so we can splice without ba +d kharma) for my $bit ( reverse 0 .. $#bits ) { ## if it'll fit in this line if( $bits[ $bit ][ 0 ]+ $bits[ $bit ][ 1 ] < $groups[-1][-1][0 +] ) { ## add it push @{ $groups[-1] }, splice @bits, $bit, 1; } } } ## display the results print $string; for my $group ( @groups ){ my $line = ' ' x 70; for my $bit ( @{ $group } ) { substr $line, $bit->[ 0 ], $bit->[ 1 ], substr $string, $bit->[ 0 ], $bit->[ 1 ]; } print $line; } __END__ c:\test>700817.pl 1234567890123456789012345678901234567890123456789012345678901234567890 23 89 4567890 567890123 9012 567890123 012 0 234567890 56789012345 901234567 45678 123456789 45678 3456789012 890123 90123456 3456789 234 90123456 c:\test>700817.pl 1234567890123456789012345678901234567890123456789012345678901234567890 2345 890123 6789012 5678 234567 012345 2345 90 5678901234 789012345 12345 67890123 01234 90 45678901 6789 234567890 34567 0123456789 9012345 c:\test>700817.pl 1234567890123456789012345678901234567890123456789012345678901234567890 1234567 34567 234567890 9012 0 345678901 5678901 6789012 89012 567890 23456789012 567890 234567890 4567890123 789012345 0123456789 5678901234 45678901234 89012345 56789012345 c:\test>700817.pl 1234567890123456789012345678901234567890123456789012345678901234567890 123456 90 56789012 56789 567 234567 1234 234567890 67 123456789 23456 345678901 01234 678901234 1234 890 890123456 89012 678901234 45678901

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: formatting output question (use of recursive subroutine?) by BrowserUk
in thread formatting output question (use of recursive subroutine?) by rogerd

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.