Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Look ahead and join if the line begins with a +

by stephen (Priest)
on Apr 11, 2002 at 04:36 UTC ( #158225=note: print w/replies, xml ) Need Help??

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.


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!



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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://158225]
[stevieb]: one of my long-running Perl procs crashes with a segfault (because it wraps C code) is crashing after an hour or so... seeing if wrapping the cli call with valgrind will produce something useful

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2017-04-29 03:08 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (531 votes). Check out past polls.