Solved

Perl Script Hangs on Empty File

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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