it is not very easy for me yet to understand code written by others.

We all have that trouble :) Maybe this annotation will help. I'm always wary of doing this because I tend to comment on the things I think are significant, rather than on those that the reader will consider so. But Maybe it will help you.

Note: You could make @bits hold [ offset, $subsequence ]. You would then have to use length( $bit[ n ][ 1 ] ) everywhere I've used $bit[ n ][ 1 ].

#! perl -slw use strict; ## Generate some data my $string = '1234567890' x 7; ## This just generates some random subsequences as offset/length pairs ## Array of Arrays (AoA): $bits[ n ] = [ offset, length] my @bits = map [ int rand length $string, 2 + int rand 10 ], 1 .. 20; ## Sort ascending offset/descending length @bits = sort{ $a->[ 0 ] <=> $b->[ 0 ] ## sort on the offsets of $a & $b || ## And if they are equal $b->[ 1 ] <=> $a->[ 1 ] ## on the lengths (reversed} } @bits; ## Result ex: @bits =( [2,5],[2,3],[3,1][4,4],[4,3] etc. ## @groups will become an AoAoA. ## Individual offset/length pairs are moved from @bits ## into @groups[n][m] Where ## n := line of output ## m := non-overlapping pair in line ## $groups[ n ] = [ [ offset1, length1], ...], [[,],[,]],...] my @groups; ## As we're moving pairs from @bits to @groups[n] ## When @bits is empty, we know we're done. while( @bits ) { ## start a new group with the last (rightmost shortest) item in @b +its ## First time around the while loop, first line of output; 2nd tim +e 2nd line push @groups, [ pop @bits ]; ## look at each of the remaining pairs in @bits ## scanning backwards because we are removing elements from @bits ## and if we went forward, removing element i would screw up the i +ndexing ## for elements i+1 .. i+n. for my $bit ( reverse 0 .. $#bits ) { ## if it'll fit in this line ## compare the last position (offset+length) for the current b +it ## against the first position (offset) of the last element of +the ## last group (line) if( $bits[ $bit ][ 0 ]+ $bits[ $bit ][ 1 ] < $groups[-1][-1][0 +] ) { ## If it's is less (moving backward!), add it. push @{ $groups[-1] }, splice @bits, $bit, 1; } } ## When the for loop ends, we've scanned all the bits and moved ## any that will fit in the current line without overlap. ## So now, if there are any left (while(@bits)) loop back to ## push another anon array onto @group add ing the now last elemen +t ## of @bits as a starting point. Repeat the for loop. } ## 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; }

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^3: 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.