Gunnar Hjalmarsson wrote:
Debbie Cooper wrote:

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).


<code doing other stuff snipped>

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?


I don't think you need any complex data structure. A counter for each
field should do it:

    chomp( my @headers = split /\t/, <INPUT> );
    my %fields = map { $_, 0 } @headers;

    while (<INPUT>) {
        chomp;
        my @rec = split /\t/;
        for ( 0 .. $#rec ) {
            $fields{ $headers[$_] }++ if $rec[$_];
                                           ^^^^^^^^
If the field value is '0' (zero) then it won't be counted correctly.

        }
    }

    for ( keys %fields ) {
        print "The \"$_\" field is empty.\n" if $fields{$_} == 0;
    }


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