Solved

Perl Script Hangs on Empty File

Posted on 2010-09-24
5
322 Views
Last Modified: 2012-05-10
I have the attached perl script which formats a file. However if the file is empty, the perl script hangs. Can you please edit the script so it exits if the file is empty?
PostProcess.txt
0
Comment
Question by:copiacap
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:jmatix
ID: 33754457
my $input = $ARGV[0] || die "Please specify input file\n";
open(INPUT, $input) || die "Can't open input file $input: $!\n";
1 while( ($_=<INPUT>) !~ /^name\s+number\s+symbol\s+initial/ && /./);
do {
   chomp;
   my @F = split/\t/;
   print join("\t",@F[0..14])."\n";
} while( <INPUT> );
close(INPUT);
0
 
LVL 16

Accepted Solution

by:
jmatix earned 500 total points
ID: 33754603
My previous solution will exit if it finds a blank line. To exit with error if the input file is empty use the following:

my $input = $ARGV[0] || die "Please specify input file\n";
-s $input || die "Empty input file\n";
open(INPUT, $input) || die "Can't open input file $input: $!\n";
1 while( ($_=<INPUT>) !~ /^name\s+number\s+symbol\s+initial/);
do{
   chomp;
   my @F = split/\t/;
   print join("\t",@F[0..14])."\n";
} while( <INPUT> );
close(INPUT);
0
 
LVL 84

Expert Comment

by:ozo
ID: 33759855
my $input = $ARGV[0] || die "Please specify input file\n";
open(INPUT, $input) || die "Can't open input file $input: $!\n";
while( <INPUT> ){
  next unless /^name\s+number\s+symbol\s+initial/..0;
   chomp;
   my @F = split/\t/;
   print join("\t",@F[0..14])."\n";
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 33768592
that still hangs on a non-empty file that does not contain name\s+number\s+symbol\s+initial
0
 
LVL 16

Expert Comment

by:jmatix
ID: 33770893
Sorry, try this:

my $input = $ARGV[0] || die "Please specify input file\n";
-s $input || die "Empty input file\n";
open(INPUT, $input) || die "Can't open input file $input: $!\n";

while ($_=<INPUT>) {/^name\s+number\s+symbol\s+initial/ && last}

do{
   chomp;
   my @F = split/\t/;
   print join("\t",@F[0..14])."\n";
} while( <INPUT> );
close(INPUT);
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now