Solved

Perl Script Hangs on Empty File

Posted on 2010-09-24
5
328 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reading fields from the text file. 4 104
PERL export multiple query results to a JSON file 1 177
perl split by | 2 108
Perl troubleshooting error. Base64 bootstrap parameter 5 75
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

740 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