http://qs321.pair.com?node_id=164015

nlafferty has asked for the wisdom of the Perl Monks concerning the following question:

This one is a bit more tricky. This part of my program basically prints out a nice little html table with the coresponding DB values. And it totals the hours and minutes for use in the bottom table. The code seems to work fine, other than the pesky little error.
the error:
Use of uninitialized value in addition (+) at /home/httpd/cgi-bin/nick/timeclock-1.0.1/search.cgi line 260 ( +#1)
And the code:
while (my @row = $common::sql::sth->fetchrow()){ my $daily_time = sprintf("%d:%02d",$row[4],$row[5]); my ($total_minutes, $total_hours, $all_minutes, $total_time_hours, $to +tal_time_minutes); $total_minutes = $row[5]; $total_hours = $row[4] * 60; $all_minutes = $total_hours + $total_minutes; $total_time = $total_time + $all_minutes; $total_time_hours = int($total_time / 60); $total_time_minutes = $total_time % 60; $all_time = sprintf("%d:%02d",$total_time_hours,$total_time_minute +s); ## Make our HTML look better if no data. $row[0] = "&nbsp;" if($row[0] eq ""); $row[1] = "&nbsp;" if($row[1] eq ""); $row[2] = "&nbsp;" if($row[2] eq ""); $row[3] = "&nbsp;" if($row[3] eq ""); $row[4] = "&nbsp;" if($row[4] eq ""); $row[5] = "&nbsp;" if($row[5] eq ""); $row[6] = "&nbsp;" if($row[6] eq ""); my (@date_array01, $print_date, $in_time_sec, @in_time_array, $in_ +time); @date_array01 = split(/ /,$row[2]); $print_date = $date_array01[0]; $in_time_sec = $date_array01[1]; @in_time_array = split(/:/,$in_time_sec); $in_time = join(":",$in_time_array[0],$in_time_array[1]); my (@date_array02, $out_time_sec, @out_time_array, $out_time); @date_array02 = split(/ /,$row[3]); $out_time_sec = $date_array02[1]; @out_time_array = split(/:/,$out_time_sec); $out_time = join(":",$out_time_array[0],$out_time_array[1]); print <<HTML; <TR BGCOLOR="#FFFFFF"> <TD><FONT SIZE=2 FACE=ARIAL>$print_date</FONT></TD> <TD><FONT SIZE=2 FACE=ARIAL>$in_time</FONT></TD> <TD><FONT SIZE=2 FACE=ARIAL>$out_time</FONT></TD> <TD BGCOLOR="#FFFFCC"><FONT SIZE=2 FACE=ARIAL>$daily_time</FONT></ +TD> <TD><FONT SIZE=2 FACE=ARIAL>$row[6]</FONT></TD> HTML } # End of while.

Replies are listed 'Best First'.
Re: Yet another Uninitialized value
by clintp (Curate) on May 04, 2002 at 21:41 UTC
    Perhaps the error isn't where you think it is. Look:
    while (my @row = $common::sql::sth->fetchrow()){ my $daily_time = sprintf("%d:%02d",$row[4],$row[5]); my ($total_minutes, $total_hours, $all_minutes, $total_time_hours, $to +tal_time_minutes); $total_minutes = $row[5]; $total_hours = $row[4] * 60; $all_minutes = $total_hours + $total_minutes; $total_time = $total_time + $all_minutes;
    I don't see $total_time declared, and I don't see it initialized before it's used here.
      Yes. yes. I found out what it is just before I looked at your post.
      I set
      $total_time = 0; before I used it in the while statement.

      The error occurred because the very first loop has no value for
      $total_time until $total_time = $total_time + $all_minutes;

      I discovered this by putting a print statment before and after $total_time = $total_time + $all_minutes;. The first print statement only had one value and all of the others had two. Thanks
Re: Yet another Uninitialized value
by Maclir (Curate) on May 04, 2002 at 17:01 UTC
    Some preliminary questions:
    • Which line in your listing is line 260?
    • Are you running with strict and warnings?
    • What is the result of the first sprintf?
      This looks to be what is being complained about.
      $all_minutes = $total_hours + $total_minutes;
      #!/usr/bin/perl -w use strict;

      The result of this is the Total hours and total minutes of each day/row in the db
        Well, this warning means that either $total_hours or $total_minutes contains undef. Since previous line has
        $total_hours = $row[4] * 60;
        and it doesn't cause a warning then $total_hours can be excluded from suspect list. $total_minutes gets its value from $row[5] which seems to be undefined. Are you sure that your query is correct and returns correct number of rows?

        Probably fetchrow doesn't return what you expect it to return. Often to help myself debugging simular problems I use Data::Dumper to check if variables really have data I expect them to have. Try to add after line with fetchrow

        use Data::Dumper; print Dumper(\@row);
        to find out actual content of @row. Once bug is fixed you can remove this debug print.

        --
        Ilya Martynov (http://martynov.org/)