It just struck me that this is (the same as) a fixed font text justification algorithm where the As are the words, and the Bs, the number of (extra) spaces to be distributed between the words to make up the line length.

Update: A crude example:

#! perl -slw use strict; our $WIDTH ||= 80; my $buffer = ''; while( <DATA> ) { $buffer .= $_; $buffer =~ s[\s+][ ]g; $buffer =~ s[\. ][. ]g; next unless eof( DATA ) or length( $buffer ) >= $WIDTH; while( length $buffer > $WIDTH ) { my $line = substr $buffer, 0, 1+rindex( $buffer, ' ', $WIDTH-1 + )||-1,''; my $fill = $WIDTH - length $line; my $spaces = $line =~ m[(\s+)(?!$)]g; $fill = 0 unless $spaces; my $n = int $spaces / 2; while( $fill > 0 ) { $line =~ s[((?:\s+?\S+){$n})(?<=\S)(\s+)(?=\S)][ $1 . $2 . ($fill-- > 0 ? ' ' : '') ]ge; $n /= 2; } print $line; # printf "%-${WIDTH}s\t\t%d : %d\n", $line, length $line, lengt +h( $buffer ); } } print $buffer if length $buffer;; #printf "%-${WIDTH}s\t\t%d : %d\n", $buffer, 0, length( $buffer ); __DATA__ Pickering is seated at the table, putting down some cards and a tuning-fork which he has been using. Higgins is standing up near him, closing two or three file drawers which are hanging out. He appears in the morning light as a robust, vital, appetizing sort of man of forty or thereabouts, dressed in a professional-looking black frock-coat with a white linen collar and black silk tie. He is of the energetic, scientific type, heartily, even violently interested in everything that can be studied as a scientific subject, and careless about himself and other people, including their feelings. He is, in fact, but for his years and size, rather like a very impetuous baby "taking notice" eagerly and loudly, and requiring almost as much watching to keep him out of unintended mischief. His manner varies from genial bullying when he is in a good humor to stormy petulance when anything goes wrong; but he is so entirely frank and void of malice that he remains likeable even in his least reasonable moments.
C:\test>junk5 -WIDTH=80 Pickering is seated at the table, putting down some cards and a tu +ning-fork which he has been using. Higgins is standing up near him, closing two + or three file drawers which are hanging out. He appears in the morning l +ight as a robust, vital, appetizing sort of man of forty or thereabouts, dre +ssed in a professional-looking black frock-coat with a white linen collar and b +lack silk tie. He is of the energetic, scientific type, heartily, even +violently interested in everything that can be studied as a scientific sub +ject, and careless about himself and other people, including their feelings. +He is, in fact, but for his years and size, rather like a very impetuous bab +y "taking notice" eagerly and loudly, and requiring almost as much watching to + keep him out of unintended mischief. His manner varies from genial bullying w +hen he is in a good humor to stormy petulance when anything goes wrong; but + he is so entirely frank and void of malice that he remains likeable even in +his least reasonable moments. C:\test>junk5 -WIDTH=60 Pickering is seated at the table, putting down some cards and a tuning-fork which he has been using. Higgins is standing up near him, closing two or three file drawers which are hanging out. He appears in the morning light as a robust, vital, appetizing sort of man of forty or thereabouts, dressed in a professional-looking black frock-coat with a white linen collar and black silk tie. He is of the energetic, scientific type, heartily, even violently interested in everything that can be studied as a scientific subject, and careless about himself and other people, including their feelings. He is, in fact, but for his years and size, rather like a very impetuous baby "taking notice" eagerly and loudly, and requiring almost as much watching to keep him out of unintended mischief. His manner varies from genial bullying when he is in a good humor to stormy petulance when anything goes wrong; but he is so entirely frank and void of malice that he remains likeable even in his least reasonable moments. C:\test>junk5 -WIDTH=40 Pickering is seated at the table, putting down some cards and a tuning-fork which he has been using. Higgins is standing up near him, closing two or three file drawers which are hanging out. He appears in the morning light as a robust, vital, appetizing sort of man of forty or thereabouts, dressed in a professional-looking black frock-coat with a white linen collar and black silk tie. He is of the energetic, scientific type, heartily, even violently interested in everything that can be studied as a scientific subject, and careless about himself and other people, including their feelings. He is, in fact, but for his years and size, rather like a very impetuous baby "taking notice" eagerly and loudly, and requiring almost as much watching to keep him out of unintended mischief. His manner varies from genial bullying when he is in a good humor to stormy petulance when anything goes wrong; but he is so entirely frank and void of malice that he remains likeable even in his least reasonable moments.

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: Spreading out the elements by BrowserUk
in thread Spreading out the elements by Anonymous Monk

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.