It seems to me that all the bother of setting up a while loop is unnecessary if all you're doing is skipping the first line of the file, reading the second, and exiting. I might write such a sub like this:
sub check_field { open my $fh, "<", shift or die "Bleah!\n$!"; <$fh>; # Skip the unwanted line. my @fields = split /;/, <$fh>; close $fh; die "Ick!\n" unless @fields == 4; return( ($fields[2] =~ /^\d+$/) ? 1 : 0 ); }
It's not really a matter of golf, I just like the idea that if we are only reading the first two lines from a file, listing <$fh> twice instead of breaking out of a while loop after the second line is somehow preferable.
Also, I think your goal is to return true if $fields[2] matches only digits. I've used the ternary operator to ensure that undef never gets returned.
Dave
In reply to Re: advice for reading data from a file
by davido
in thread advice for reading data from a file
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |