copiacap
asked on
Perl Script Hangs on Empty File
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
PostProcess.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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";
}
open(INPUT, $input) || die "Can't open input file $input: $!\n";
while( <INPUT> ){
next unless /^name\s+number\s+symbol\s
chomp;
my @F = split/\t/;
print join("\t",@F[0..14])."\n";
}
that still hangs on a non-empty file that does not contain name\s+number\s+symbol\s+i nitial
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);
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\
do{
chomp;
my @F = split/\t/;
print join("\t",@F[0..14])."\n";
} while( <INPUT> );
close(INPUT);
open(INPUT, $input) || die "Can't open input file $input: $!\n";
1 while( ($_=<INPUT>) !~ /^name\s+number\s+symbol\s
do {
chomp;
my @F = split/\t/;
print join("\t",@F[0..14])."\n";
} while( <INPUT> );
close(INPUT);