Debbie Cooper wrote:
I'm pretty new to Perl but with the help of this list I've been able to come
up with a few helpful scripts.  This time I'm trying to read through a
tab-delimited text file with the first row containing headers.  I want to
print out any field/column name where the entire field is null (meaning
there is no value for that field for any record in the file).  Since I'm
still experimenting with Perl I've started out with the following just to
test my Perl knowledge:

#!perl
use warnings;
use strict;

 open INDEX, "> fieldcontents.txt" or die "can't create fieldcontents.txt
$!";
 open INPUT, "<lv1.txt" or die "can't open data file: $!";
  {

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

        print INDEX "$fields[0]\n";
    }
    close INPUT;
  }

  close INDEX;

This works ok but now I'm ready for the next step.

I'm thinking that if I read each column into an array element I can then use
parallel arrays to search through the array of fields to get the element
subscript where all values are null and then get the corresponding field
name from another array.  But I don't know the syntax to search for a null
field value and I'm not sure how to search the array (which is really a
matrix) so that I search all of $fields[0] then all of $fields[1] and so on.
Any suggestions?

Perhaps you want something like this:

#!/usr/bin/perl
use warnings;
use strict;

# get the first record which contains the field names
chomp( my @headers = split /\t/, <DATA> );

my %count;
while ( <DATA> ) {
    chomp;
    my @data = split /\t/, $_, -1;
    $count{ $headers[ $_ ] } += length $data[ $_ ] for 0 .. $#headers;
    }

for ( @headers ) {
    print "Field '$_' is empty.\n" unless $count{ $_ };
    }


__DATA__ date name colour size width 10/30/04 pants tan 36 01/12/04 shirt red L 05/03/04 cup white



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