deleting empty lines from a variable containing \n's

deleting empty lines from a variable containing \n's

Post by Tad McClel » Fri, 05 Nov 2004 07:48:50



By deleting the newline character too.




You should always, yes *always*, check the return value from open():

open(FILE, 'someFile') or die "could not open 'someFile' $!";




$file =~ s/^#+\n//mg;


--
Tad McClellan SGML consulting
XXXX@XXXXX.COM Perl programming
Fort Worth, Texas
 
 
 

deleting empty lines from a variable containing \n's

Post by spamsake » Fri, 05 Nov 2004 07:59:42

I read a file and store it in a scalar variable...the file contains
lines with ### i need to delete those lines. When i substitute them i
get an empty line but not a deletedline. How do i achieve this ?

Code

undef $/;
open(FILE,"someFile");
$file = <FILE>;
$file =~ s/^#+$//mg;
print $file;

Result
If someFileContained
1) myCode
2) ###
3) furtherCode

The output is
1) myCode

2) furtherCode

I want output like
1) myCode
2) furtherCode

regards
-Machoq