Click to See Complete Forum and Search --> : Problem with Blank Lines


kimskams80
12-17-2008, 05:25 AM
Hi

I have to process HTML files. The problem that I am facing is that sometimes there are BLANK LINES between paragraphs or lines and PERL interprets each blank line as a sentence (because I am processing these files by sentences)... I don't know what condition I can put to avoid these blank lines.I have already tried $line="" etc but it does not work...any help is welcomed..

Thanks in adv

dragle
12-17-2008, 10:14 AM
Did you really have:
$line = ""
That wouldn't work for two reasons; you're using the assignment operator (=) instead of the equality operator (==), and for string comparisons you should be using eq anyway, i.e.:
$line eq ""
or for that matter, just testing $line itself should be sufficient.

But my guess is that you're not chomping your lines (so they actually include line feeds), or there are some actual real spaces in the lines, or some combination of both. If the line has line feeds or white space then a comparison to "" will be false. Try:
#!/usr/bin/perl

use strict;
use warnings;

while (<DATA>) {
next if (/^\s*$/);
print;
}

# Above prints:
# This is
# some data
# separated by
# blank lines.

__DATA__
This is
some data

separated by

blank lines.

That will eliminate lines that are truly blank (nothing but a line feed) as well as those that have only white space.

HTH