You probably don't appreciate the fact that I'm including the prior message
in this latest email.  But I want to let you all know how much stuff like
this helps someone like me who's learning Perl.  To understand how my code
could be more efficient is a great learning tool.  I can look up things in
books all day but I usually won't know what is the "best" or "better" way to
do something from that.  Thank you for taking the time to go through my code
and comment on the pieces that weren't needed and, even better, to tell me
why they weren't needed.

I'm liking this Perl stuff more and more.

Thanks,
Debbie

-----Original Message-----
From: John W. Krahn [mailto:[EMAIL PROTECTED]
Sent: Tuesday, November 30, 2004 4:40 PM
To: Perl Beginners
Subject: Re: Search Tab-delimited file for Null fields


Debbie Cooper wrote:
> Thanks to everyone for their help on this problem.  I finally ended up
using
> the following:
> #!perl
> use warnings;
> use strict;
>
> my @empty = ();
> my @headings = ();

Aggregate variables created with my() are empty by default so assigning an
empty list to them is redundant.


> my $sum;
>
>  open INDEX, "> fieldcontents.txt" or die "can't create fieldcontents.txt
> $!";
>  open INPUT, "<lv1.txt" or die "can't open data file: $!";
>   {
>
>     while (<INPUT>) {
>         if ($. == 1) {
>           chomp;                      # remove newline
>           @headings = split /\t/;  # get each of the headings
>         @empty = @headings;
>       }
>         next if $. == 1 ;
>         chomp;                      # remove newline

There is no need to chomp() or compare $. twice.

     while (<INPUT>) {
         chomp;                      # remove newline
         if ($. == 1) {
           @headings = split /\t/;  # get each of the headings
          @empty = @headings;
           next;
        }


>         my @fields = split /\t/;    # get each of the fields
>
>       for (my $i = 0; $i <= $#fields; $i++) {

The usual Perl way to write that is:

        for my $i ( 0 .. $#fields ) {


>               if (length($fields[$i]) >= 1) {                 #look for 
> fields with no
> value
>
>                   $empty[$i] = "";                          #assign flag to 
> new array
>               }
>       }
>     }
>
>       foreach (@empty){
>               if( $_ ){
>                       print INPUT "$_\n";
>               }
>       }
>
>       close INPUT;
>   }
>
>   close INDEX;


John
--
use Perl;
program
fulfillment



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to