Main Content

Remember to process blank lines

Archive - Originally posted on "The Horse's Mouth" - 2006-01-31 06:58:16 - Graham Ellis

I've got a Perl program that processes a data file 200 lines long.

15 of the lines are comments that start with a # (I test for those using ($line =~ /^#/), and 181 of the lines contain real data - in other words they start with a character that's not a # - my regular expression match reads ($line =~ /^[^#]/).

Whole file processed? No! There are 200 lines in the file but I've only processed 196. Although you'll think inventively that a line starts with either a hash or a character that's not a hash, there's a third case - a line that doesn't start with any character at all, i.e. a blank line. That's where my extra four lines have gone.

Rather than using two if statements to process all the lines, and match up the conditions, I should have used if and else. That way I can have my program securely find the "others" rather than having to work out potentially buggy code to do so.

Being Perl (where there's half a dozen ways to do anything), there are other good solutions such as writing the same condition twice but using the !~ operator in place of =~ to get the inverse set, or using an unless statement, or doing the test just once and saving the result as a boolean - $iscomment = ($line !~ /^\s*[^#]/);.