Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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.

stephen

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

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


Comment on Re: Look ahead and join if the line begins with a +
Select or Download Code
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: 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.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://158225]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-12-21 11:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (104 votes), past polls