in reply to Single newline removal when line exceeds x characters

Here's my solution to your little problem -

use strict; my $prev_length = 0; my $threshold = 40; my $first_line = 1; while (<DATA>) { chomp; # s/^\s+//g; # s/\s+$//g; my $curr_length = length($_); if ($prev_length > $threshold && $curr_length > 0) { print " "; } elsif ($first_line) { $first_line = 0; } else { print "\n"; } print "$_"; $prev_length = length($_); } print "\n"; __DATA__ This is a longish line whose linebreak should be removed so that this line runs together with it. The preceding double linebreak should be conserved. This little line and this one should stay separated.
And the output is as expected -
This is a longish line whose linebreak should be removed so that this +line runs together with it. The preceding double linebreak should be conserved. This little line and this one should stay separated.

Replies are listed 'Best First'.
Re: Re: Single newline removal when line exceeds x characters
by tachyon (Chancellor) on Oct 20, 2003 at 07:51 UTC

    Here's one with a regex and a lookahead assertion.

    local $/; $_ = <DATA>; my $wrap_min = 20; s/^(.{$wrap_min,})\n(?=\S)/$1 /gm; print; __DATA__ This is a longish line whose linebreak should be removed so that this line runs together with it. The preceding double linebreak should be conserved. This little line and this one should stay separated.

    Added space after $1 thanks Roger

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Nice solution :-D. But you might want to add a space after $1. Otherwise the output becomes "This is a longish line whose linebreak should be removedso that this line runs together with it." where there is no space between the removed and so.