Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check for an empty line 1

Status
Not open for further replies.

blues77

Programmer
Jun 11, 2002
230
CA
Hello,

I need a way to check to see if there is a blank line in a text document being read. Below is the code I have at the moment but it doesn't seem to be working. Any suggestions?
Also should I be using the "last" statement to break out of the while loo. As you can see I'm quite new to PERL.

Code:
use strict;

#print "$ARGV[0]";
open(MY_FILE1,$ARGV[0]); 


while(<MY_FILE1>)
{
	chomp;
	my $line1 = $_;

	open(MY_FILE2,$ARGV[1]);
	while(<MY_FILE2>)
	{
		my $line2 = $_;
		if ($line2 eq "")
		{
			print "Empty Line";
			last;
		}
		else
		{
			print ("$line1 $line2");			
		}
	}
	close MY_FILE2;
}

close MY_FILE1;
close MY_FILE2;
 
Does your "empty line" contain whitespace?
Also, you forgot to [red]"chomp"[/red] your input from "MY_FILE2".
To check for lines that have whitespace you can use a regex like this:
Code:
  if(/^\s*$/) { print "Empty Line"; last; }


Trojan.
 
Go with the trojan man's, an "empty" line may contain whitespace, but it doesn't have to.

- Andrew
Text::Highlight - A language-neutral syntax highlighting module in Perl
also on SourceForge including demo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top