Solved

Perl Script Hangs on Empty File

Posted on 2010-09-24
5
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remove Malware code from PHP file 6 106
Is there a file descriptor created for logfile using by logmsg within Perl script 4 145
pattern matching in perl 2 115
Perl Untar File 1 76
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 …
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…

737 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