Perl: testing for blank lines
i'm trying to find blank lines in a text file
this is the code Code:
use strict; Code:
1 i'm pretty sure that if ($_ == "") is the wrong way to test for an empty string, what should i be doing instead? edit: i'm kinda confused also, because i think blank lines are actually nothing, followed by the new like character, but i still get the "it's blank" message |
Hi,
Try this: Code:
#!/bin/perl Hope this helps. |
thanks for that, i was pretty sure that i was checking for empty lines in the wrong way
|
You might want to try:
Code:
/^\s*$/ Brian |
You used the == to test for an empty string, but this is the numerical equality test. You should use eq for strings. Also $_ will not equal "" for a blank line - it will equal "\n". If you want to get rid of the newline character before testing, use chomp first.
Code:
while (<FILE>) { Code:
while (<FILE>) { P.P.S. when calling open, you don't need to separate the <. You can call it like this: open(FILE, "<myfilename") || die ...; |
All times are GMT -5. The time now is 01:31 AM. |