http://www.perlmonks.org?node_id=158225


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

The other methods listed here are probably more efficient, especially if you're going to do something with the lines individually later. However, since you're preloading the file into memory as it is, here's something simpler:

use strict; my $all_text; { local($/); $all_text = <NETLIST>; } $all_text =~ s/\n\+/ /gs; print $all_text;

You might want to check out Text::Wrap for reflowing it.

stephen

Update: Thanks to japhy for pointing out a mental misstep on my part...

Update 2: Thanks to broquaint for a reminder on local()...

Replies are listed 'Best First'.
Re: Re: Look ahead and join if the line begins with a +
by japhy (Canon) on Apr 11, 2002 at 04:46 UTC
    The $/ variable is not filehandle-based (yet -- wait for Perl 6). You'll want to local()ise it in a block, read from the filehandle, and then leave the scope.

    _____________________________________________________
    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:??;

Re: Re: Look ahead and join if the line begins with a +
by broquaint (Abbot) on Apr 11, 2002 at 10:36 UTC
    Just a small note - local() automatically undef()ines whatever gets passed to it.
    $foo = "I am a package var, yes I am"; { local $foo; print "\$foo is no more\n" if not defined $foo; } print qq(\$foo returns!\n) if defined $foo; __output__ $foo is no more $foo returns!

    HTH

    broquaint

Re: Look ahead and join if the line begins with a +
by Smylers (Pilgrim) on Apr 11, 2002 at 08:52 UTC

    If you can fit all the lines and actually want to process them as separate lines then you can use a negative lookahead assertion to split just on linebreaks that aren't followed by plus signs:

    { local $/; foreach (split /\n(?!\+)/, <>) { s/\n\+/ /g; print "$_\n"; } }

    Then you just have to substitute out the internal linebreaks and pluses that are left. (You may not want the space in there depending on your data.)

    Note that as a side-effect, the split has chomped the input, so a \n may be needed in the output.