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

Re: remove single blank lines?

by Coruscate (Sexton)
on Dec 08, 2003 at 22:09 UTC ( [id://313269]=note: print w/replies, xml ) Need Help??


in reply to remove single blank lines?

I bid you welcome to the wonderful world of regular expressions (aka regexes). More information can be learned about these from perlre. Here's my version:

my $string = q{ this is a sample text. all single blank lines will be removed. all double, triple, or more blank lines will not be touched. }; $string =~ s#\n(\n+)#"\n" x (length($1)>1 ? length($1)+1 : length($1))#eg; print $string; __END__ this is a sample text. all single blank lines will be removed. all double, triple, or more blank lines will not be touched.

Replies are listed 'Best First'.
Re: Re: remove single blank lines?
by hmerrill (Friar) on Dec 08, 2003 at 22:23 UTC
    That may work, but that regex hurts my head. The only way I would use that code is if it were commented well as to what each part of the regex does - even then I might not use it.

    A much clearer, albeit more code and more steps method, would be to:
    1. read from the input file, and 2. write only lines that you want to write to a temporary output file, and 3. close the input file 4. rename the temporary output file to the input file's name.
    This method would allow you to read ahead one (or more than one) line to see if there are 2 more more blank lines together.

    Just my preference, but IMHO much clearer than trying to decifer that regex. My choice is to always make my code as understandable to *everyone* (including newbies) as possible. Obfuscated regex's do more to scare people away from Perl. Just my 2c.

    HTH.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-18 17:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found