First of all, I can't even see how you managed to get this code to run. You've got C++ comments in there. Assuming this was a typo caused by sleep deprivation, we move on to the next issue:

use warnings;

use strict;

Had you done that, you'd have found that your output file wasn't open when you tried to write to it. This could have saved you some tearing out of hair. So use something like open(OUTFILE, ">", "outfile.txt");

Next we've got the regex. You were on the right track, but you were trying to do too much. All you needed for the newline marker was \n. However, there were a couple of other problems. The parentheses around the $1 would be added to your text; you need to eliminate them. Finally, the regex was slightly off. Basically, you wanted to capture any characters, up to the newline. If there was a period, you wanted to ignore that (not make the substitution), but you still needed to capture it. So you get:

$contents =~ s/(.*?[^\.])\n/$1/mg;

I hope you're able to sleep now... but knowing programmers, you'll probably think to yourself, "just one more little change here...".

Update: [id://McDarren]'s regex is correct; mine lacks the double-quote, which would create problems with the second item.


In reply to Re: replacement of newlines by spiritway
in thread replacement of newlines by neodymium

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.