That might present a problem, as some spaces are actual delimiters, and others are going to be literal text. If you treat both the same way (split on plain spaces and reassemble all the "words"), you may end up splitting words that happened to span a 27-char limit.

The way the problem was stated it seemed to me that you can guarantee that each 'line' ends in a space. If that is correct then you know each 'line' is the concatenation of a number of 'chunks', that is no 'chunk' can belong to two different lines. Is that not correct?

That is not, of course, to say that every space is the delimiter of a 'line' (otherwise it would be simple). My code did take into account the fact that spaces have two different meanings. While I did forget to allow for adding one for the extra space a working version is quite close to my original code:

use strict; use warnings; $/ = ' '; my $line = ''; print "|012345678901234567890123456|\n"; while(my $chunk = <DATA>) { if((length($line) + length($chunk)) > 28) { # Remove the delimiter space and pad out chop($line); $line .= ' 'x(27-length($line)); # Do the line processing print "|$line|\n"; $line = ''; } $line .= $chunk; } # Process the final line chop($line); $line .= ' 'x(27-length($line)); print "|$line|\n"; __END__ 012345678-A1234567 INCL.EUR 3,31 MWST JULI MONATL. GEB HR T-DSL FLAT 0 +1.07.04-3

Gives

|012345678901234567890123456| |012345678-A1234567 INCL.EUR| |3,31 MWST JULI MONATL. GEB | |HR T-DSL FLAT 01.07.04-3 |

If however my assumption about spaces at the end of each line is wrong (for example if there could be words that are longer than 27 characters without a space) then a simple if statement will take care of that, something like:

while(my $chunk = <DATA>) { if(length($chunk) > 27) { # Process holdover line process_line($line) if($line); $line = ''; while($chunk =~ s/^(.{27})//) { process_line($1); } $line = $chunk; } elsif((length($line) + length($chunk)) > 28) {

It is true that this is a more simplistic approach than using a "negative lookahead assertion", but there again I don't know how one of them works :-)


In reply to Re^3: splitting text into lines -- code -> regex by hawtin
in thread splitting text into lines -- code -> regex by theorbtwo

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.