Solved

Perl Script Hangs on Empty File

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl search and replace 6 165
crawling gofundme 4 119
git fat pull happens nothing in centos 6.3 python 2.6.6 33 207
Having a problem with ActiveState Perl on 64-bit Windows 7 3 68
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 …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

937 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

10 Experts available now in Live!

Get 1:1 Help Now