in reply to Look ahead and join if the line begins with a +

Here are several approaches; they are of varying degrees of difficulty.
# simplistic my @lines = scalar <FILE>; while (<FILE>) { if (s/^\+//) { $lines[-1] .= $_ } else { push @lines, $_ } } # fancy my @lines; while (<FILE>) { if (s/^\+//) { $lines[@lines && -1] .= $_ } else { push @lines, $_ } } # compact (UPDATED: 1 -> -1) my @lines; $lines[s/^\+// ? @lines && -1 : @lines] .= $_ while <FILE>;

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: Look ahead and join if the line begins with a +
by tachyon (Chancellor) on Apr 11, 2002 at 09:26 UTC

    Uhm, these are all broken. You are forgetting to remove the trailing newline before you concatentate. See Re: (MeowChow) Re2: Look ahead and join if the line begins with a + PS The compact example is even more broken - try running it on the sample data set.

    my @lines = s//join'',<DATA>/e ? s/\n\+//g ? split "\n" : ($_) :($_);

    cheers

    tachyon

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

(MeowChow) Re2: Look ahead and join if the line begins with a +
by MeowChow (Vicar) on Apr 11, 2002 at 06:33 UTC
    # vulgar my @lines; $lines[@lines - s/^\+//] .= $_ while <FILE>;
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print

      Ahmm

      # vulgar AND broken my @lines; $lines[@lines - s/^\+//] .= $_ while <DATA>; print @lines __DATA__ This +is +supposed +to +be +one +line! Oops

      I'm sure that this is what you meant, but even this is broken as it will possibly remove the last char in the file if their is no trailing newline.

      $lines[@lines - s/^\+//] .= substr $_,0,-1 while <DATA>;

      This will work:

      $lines[ @lines + 1 - s -^\053|\012--g ] .= $_ while <DATA>;

      cheers

      tachyon

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

        I based my code on japhy's solutions, none of which were stripping newlines, probably because Rhodium didn't specify if he wanted them removed, or the lines to be simply concatenated as-is into single list items.
        $lines[ @lines + 1 - s -^\053|\012--g ] .= $_  while <DATA>;
        This fails by adding an undef element to the array if a newline is not the final character in the file. Changing my original easy enough though, without any regex or substr nastiness:
        chomp, $lines[@lines - s/^\+//] .= $_ while <DATA>;
           MeowChow                                   
                       s aamecha.s a..a\u$&owag.print
Re: Re: Look ahead and join if the line begins with a +
by Rhodium (Scribe) on Apr 11, 2002 at 14:56 UTC
    Kudos japhy
    Your simplistic takes home the prize.. but you forgot to remove \n..
    @cleanet = scalar <NETLIST>; while (<NETLIST>) { if (s/^\+// or s/^\*\+//) { $cleanet[-1] =~ s/\n/ /; $cleanet[-1] .= $_ } else { push @cleanet, $_ } }
    Thanks so much, now how do I get this back to where I started from.. After 80 characters and NOT on a word(something separated with a space) break it into two lines and put back the "+"?
    Thanks again.
      Of course, that simplifies to   $cleanet[-1] =~ s/\n/ $_/;,  but see merlyn's solution below.

        p