Dear Monks, I parse a file of the following structure:
Titel Text (A12-3) 3-123.7 Just another text 3-123.8 Some more text A12.34 Another item B56.78 Yet another item Another Titel Text (B23-9) 1-22a.b Just another text 2-3cd.e Some more text W12.34 Another item Z56.78 Yet another item
The lines with parenthesis at the end are to become the titels, the other lines should be linked to these titels this way:
Titel Text (A12-3);3-123.7 Just another text Titel Text (A12-3);3-123.8 Some more text Titel Text (A12-3);A12.34 Another item Titel Text (A12-3);B56.78 Yet another item
As you see some the "other lines" should begin with the certain pattern, sometimes they do not and build a single line. I tried to break these lines with a newline character in the following way (thanks to toolic and Marschall since I used some fragments from their earlier advices) but the actual script seems to ignore the added newline.
use strict; use warnings; my $outcome; my $previous; while(<DATA>) { $outcome = ""; chomp; $_=~ s/\s(\d\-\d\w{2}(\.\w+)?)/\n$1/g; $_=~ s/\s([A-Z]\d{2}(\.\d+)?)/\n$1/g; if (/\s?\d\-\d\w{2}(\.\w+)?.+|\s?[A-Z]\d{2}(\.\d+)?.+|\(\w+\-\ +d+\)$/) { if (/\(\w+\-\d+\)$/ ) { $previous = $_; } else { $outcome = "$previous;$_"; } $outcome=~s/^\s+$//g; print "$outcome\n"; } } __DATA__ Titel Text (A12-3) 3-123.7 Just another text 3-123.8 Some more text A12.34 Another item B56.78 Yet another item Another Titel Text (B23-9) 1-22a.b Just another text 2-3cd.e Some more text W12.34 Another item Z56.78 Yet another item Some trash Some trash
Where do I make the mistake(s)? I work on Win32 with ActivePerl distribution. Thank you very much in advance! VE

In reply to Insert newline 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.