amarceluk has asked for the wisdom of the Perl Monks concerning the following question:

Everyone's answers to my first question on using regexes to remove blank lines were very helpful. Unfortunately, I have this new and annoying problem: when I do anything akin to
$lines =~ s/\n\n/\n/gms;
only the last occurance of \n\n in the file is changed; the rest of the blank lines stay the same.

Is there a Perl explanation for this, or do I just have to beat my computer with a stick?

Replies are listed 'Best First'.
Re: Another Problem Removing Blank Lines
by tadman (Prior) on May 21, 2002 at 17:45 UTC
    First, if you're using DOS-type files, the lines are separated by "\r\n" instead of just "\n". You might have to tackle this as well. Of course, were you on one of those systems, your $/ would be set accordingly. Try this?
    $lines =~ s#($/)+#$/#g;
    The /m and /s flags seem to be a little extraneous given your specific circumstances. /m is used to modify the behaviour of ^ and $ within the regex, /s to modify how . is processed. See perlre for more details.

    As a note, this is virtually identical to some of the replies you received for remove blank lines with regex, so why you posted again is unclear.
      All the previous responses do work, they just only work on the last two lines of the file. I was wondering if there was a simple Perl-related explanation for it.

      I'm sorry if I'm posting too much, I don't mean to be annoying. I debated long and hard over whether to post again, and finally decided to because otherwise I'd just sit around worrying about it.
(jeffa) Re: another problem with the blank line regex
by jeffa (Bishop) on May 21, 2002 at 17:58 UTC
    I like using tr with the 'squash' option for these problems:
    $lines =~ tr/\n//s;
    <caveat> i don't do DOS</caveat> ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      i don't do DOS

      Why not?
      $lines =~ tr/\r//d; $lines =~ tr/\n//s; # or this? $lines =~ tr/\r?\n//s;
      I don't know how DOS newlines look, but wouldn't "\n\n" be "\r\n\r\n"?

      -xtype
Re: another problem with the blank line regex
by yodabjorn (Monk) on May 22, 2002 at 04:32 UTC
    $foo =~ tr/\cM\cJ//d ; # remove all platforms version of a newline ..
    From perldoc perlop :
    All systems use the virtual ""\n"" to represent a line terminator, called a "newline". There is no such thing as an unvarying, physical newline character. It is only an illusion that the operating system, device drivers, C libraries, and Perl all conspire to preserve. Not all systems read ""\r"" as ASCII CR and ""\n"" as ASCII LF. For example, on a Mac, these are reversed, and on systems without line terminator, printing ""\n"" may emit no actual data. In general, use ""\n"" when you mean a "new­ line" for your system, but use the literal ASCII when you need an exact character. For example, most networking protocols expect and prefer a CR+LF (""\015\012"" or ""\cM\cJ"") for line terminators, and although they often accept just ""\012"", they seldom tolerate just ""\015"". If you get in the habit of using ""\n"" for networking, you may be burned some day.
update Re: another problem with the blank line regex
by amarceluk (Beadle) on May 22, 2002 at 23:26 UTC
    I posted:
    Is there a Perl explanation for this, or do I just have to beat my computer with a stick?
    Apparently I do need to beat my computer with a stick. I copied all the relevant files to my home computer and it works fine. So tomorrow I get to figure out what's wrong with my work computer, but now I know it's not the Perl that's wrong. Anyway, thank you to everyone who responded, I learned a lot of good Perl stuff as a result of this annoying computer glitch!