T Andersen
T Andersen

Reputation: 3

How do I fix the code for server error "Use of uninitialized value XX in numeric lt(<)"?

Cleaning up some old code on server ... getting an error on line 228 ... pulling out snippet ... the while ($i < $num_cols) is the line with the error AH01215 "Use of uninitialized value $num_cols in numeric lt(<) at"

I added in the my and attempted checking for not defined, the code works, but I would like to get the code working without web server error log messages.

I have several of these "Use of uninitialized" errors, hoping seeing how to fix this will help me with the rest.

            $sth = $h->prepare($sel);
            if ($sth == 0) {
                    print "<XMLRSSQLERROR>ERROR: $DBI::errstr</XMLRSSQLERROR>\n";
                    exit;
            }
            if (!$sth->execute) {
                    print "<XMLRSSQLERROR>ERROR: $DBI::errstr</XMLRSSQLERROR>\n";
                    exit;
            }

            my $num_cols = $sth->{NUM_OF_FIELDS};

            #Start the XML output
            #Start the RS section and add the table name
            print "<RS>\n";
            print "$table\n";

            #Start the SCHEMA section
            print "<SCHEMA>\n";

            my @columns = @{$sth->{NAME}};
            my @type = @{$sth->{TYPE}};

            my $i = 0;
            while ($i < $num_cols) {
                    print "<$columns[$i]>";
                    if (($type[$i] == 1)  or ($type[$i] == 12) or ($type[$i] == -1)) {
                            print "char";
                    } elsif (($type[$i] == 4) or ($type[$i] == 5) or ($type[$i] == -6)) {
                            print "int";
                    } elsif (($type[$i] == 2) or ($type[$i] == 6) or ($type[$i] == 7) or ($type[$i] == 8)) {
                            print "float";
                    } elsif (($type[$i] == 11) or ($type[$i] == 10) or ($type[$i] == 9)) {
                            print "datetime";
                    } else {
                            print "$type[$i]"
                    }
                    print "</$columns[$i]>\n";
                    $i += 1;
            }   

            #End the SCHEMA section
            print "</SCHEMA>\n";

Upvotes: 0

Views: 64

Answers (2)

Dave Cross
Dave Cross

Reputation: 69244

As others have said, it seems weird that $sth->{NUM_OF_FIELDS} is undefined. I'd definitely want to find out what's going on there.

But it looks to me like you don't need that at all. It's only there because you're using a while loop that should probably be written as a foreach loop. You could remove:

my $num_cols = $sth->{NUM_OF_FIELDS};

And replace:

while ($i < $num_cols) {

with:

foreach my $i (0 .. $#columns) {

Upvotes: 0

brian d foy
brian d foy

Reputation: 132802

The defined-or can set a default value:

 my $num_cols = $sth->{NUM_OF_FIELDS} // 0;

There are various other tricks, but most of those are different ways to checking values are what you expect before you use them.

But, without seeing your SQL statements or knowing which DBI driver you are using, there's not much we can do to guess. I'd definitely want to know why that value has undef.

Upvotes: 1

Related Questions