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.
| [reply] [d/l] |
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.
| [reply] |
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)
| [reply] [d/l] |
$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 | [reply] [d/l] |
$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.
| [reply] [d/l] |
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!
| [reply] |