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

In the process of converting my script to run under strict I lost one of my most important operations. The variable $all_time is suposed to be a total of all times in my array. Anyone see the error? The variable is produced and called in &admin_results and &normal_results
#!/usr/bin/perl -w use CGI qw/:standard/; use strict; require "common.sub"; #use CGI::Carp qw(fatalsToBrowser); #warn "this is a complaint"; #die "But this is serious"; print header; ####### SQL CONNECT &common::sql::Create_DB_Connection; ################## BEGIN DETERMINE ADMIN OR NORMAL USER my ($username, $syear, $smonth, $sday, $eyear, $emonth, $eday, $start_ +date, $end_date); if ($common::auth::user eq "$common::auth::admin"){ $username = &common::sql::filter(param("username")); $syear = &common::sql::filter(param("syear")); $smonth = &common::sql::filter(param("smonth")); $sday = &common::sql::filter(param("sday")); $eyear = &common::sql::filter(param("eyear")); $emonth = &common::sql::filter(param("emonth")); $eday = &common::sql::filter(param("eday")); $start_date = "$syear-$smonth-$sday"; $end_date = "$eyear-$emonth-$eday"; ##### SUB CALLS - This allows you to list all users ##### if ($username eq ""){ &get_all_users; &admin_results; }else{ &get_info; &admin_results; } }elsif ($common::auth::user eq ""){ &common::auth::print_error; }else{ $username = $common::auth::user ; $syear = &common::sql::filter(param("syear")); $smonth = &common::sql::filter(param("smonth")); $sday = &common::sql::filter(param("sday")); $eyear = &common::sql::filter(param("eyear")); $emonth = &common::sql::filter(param("emonth")); $eday = &common::sql::filter(param("eday")); $start_date = "$syear-$smonth-$sday"; $end_date = "$eyear-$emonth-$eday"; #### SUB CALLS &get_info; &normal_results; } ################## END DETERMINE ADMIN OR NORMAL USER # Disconnect from the database $common::sql::sth->finish; $common::sql::dbh->disconnect; ################ BEGIN GET INFO SUBROUTINE sub get_all_users{ $main::SQL="SELECT oid,* FROM timeclock WHERE start_stamp >= '$start +_date' AND end_stamp <= '$end_date' ORDER BY username"; &common::sql::Do_SQL; } ################ END GET INFO SUBROUTINE ################ BEGIN GET INFO SUBROUTINE sub get_info{ $main::SQL="SELECT oid,* FROM timeclock WHERE username = '$username' + AND start_stamp >= '$start_date' AND end_stamp <= '$end_date' ORDER +BY start_stamp"; &common::sql::Do_SQL; } ################ END GET INFO SUBROUTINE ################ BEGIN ADMIN RESULTS SUBROUTINE sub admin_results{ my ($all_time, $search_name); print <<HTML; <HTML><HEAD><TITLE>secret.tld - Timeclock Search</TITLE></HEAD> <BODY BGCOLOR="#FFFFFF"> <CENTER> <a href="$common::links::home_link">Home</A> | <a href="$common::links::timeclock_link">Timeclock Administration</A> </CENTER><BR> <CENTER><FONT SIZE=3 FACE=ARIAL>Search Results for $username, $star +t_date Through $end_date.</FONT></CENTER> <HR WIDTH=80%> <P> <CENTER><TABLE BORDER=1 CELLSPACING=0> <TR> <TD BGCOLOR="#eecb27" COLSPAN=6><FONT FACE=ARIAL> <CENTER><B>secret.tld - Employee Timeclock Database</B></CENTE +R> </FONT></TD> </TR> <TR> <TD BGCOLOR="eecb27"> <FONT SIZE=2 FACE=ARIAL><CENTER><B>Date</B></CENTER></FONT></TD> <TD BGCOLOR="eecb27"> <FONT SIZE=2 FACE=ARIAL><CENTER><B>Username</B></CENTER></FONT>< +/TD> <TD BGCOLOR="eecb27"> <FONT SIZE=2 FACE=ARIAL><CENTER><B>Clocked In</B></CENTER></FONT +></TD> <TD BGCOLOR="eecb27"> <FONT SIZE=2 FACE=ARIAL><CENTER><B>Clocked Out</B></CENTER></FON +T></TD> <TD BGCOLOR="eecb27"> <FONT SIZE=2 FACE=ARIAL><CENTER><B>Daily Total</B></CENTER></FON +T></TD> <TD BGCOLOR="eecb27"> <FONT SIZE=2 FACE=ARIAL><CENTER><B>Notes</B></CENTER></FONT></TD +> </TR> HTML 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, $total_ti +me_hours, $total_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 ""); $search_name = $row[1]; $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>$row[1]</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. if($search_name eq ""){ print<<HTML; <TR><TD colspan=6><center>The username <B>$username</B> was not found< +/center></TD></TR> HTML } print<<HTML; </TR><tr> <td BGCOLOR="#eecb27" colspan=3></TD> <td><B>Total Time:</B></TD><td bgcolor=#FFFF99><B>$all_time</B></TD><t +d BGCOLOR="#eecb27"></TD></TR></TABLE></CENTER> <P> <HR WIDTH=80%> </BODY></HTML> HTML } # End of normal_results subroutine ################ END ADMIN RESULTS SUBROUTINE ################ BEGIN NORMAL RESULTS SUBROUTINE sub normal_results{ my $all_time; print <<HTML; <HTML><HEAD><TITLE>secret.tld - Timeclock Search</TITLE></HEAD> <BODY BGCOLOR="#FFFFFF"> <CENTER> <a href="$common::links::home_link">Home</A> | <a href="$common::links::timeclock_link">Timeclock</A> | <a href="$common::links::searchform_link">Search</A> </CENTER><BR> <CENTER><FONT SIZE=3 FACE=ARIAL>Search Results for $username, $star +t_date Through $end_date.</FONT></CENTER> <HR WIDTH=80%> <P> <CENTER><TABLE BORDER=1 CELLSPACING=0> <TR> <TD BGCOLOR="#eecb27" COLSPAN=5><FONT FACE=ARIAL> <CENTER><B>secret.tld - Employee Timeclock Database</B></CENTE +R> </FONT></TD> </TR> <TR> <TD BGCOLOR="eecb27"> <FONT SIZE=2 FACE=ARIAL><CENTER><B>Date</B></CENTER></FONT></TD> <TD BGCOLOR="eecb27"> <FONT SIZE=2 FACE=ARIAL><CENTER><B>Clocked In</B></CENTER></FONT +></TD> <TD BGCOLOR="eecb27"> <FONT SIZE=2 FACE=ARIAL><CENTER><B>Clocked Out</B></CENTER></FON +T></TD> <TD BGCOLOR="eecb27"> <FONT SIZE=2 FACE=ARIAL><CENTER><B>Daily Total</B></CENTER></FON +T></TD> <TD BGCOLOR="eecb27"> <FONT SIZE=2 FACE=ARIAL><CENTER><B>Notes</B></CENTER></FONT></TD +> </TR> HTML 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, $total_ti +me_hours, $total_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. print<<HTML; </TR><tr> <td BGCOLOR="#eecb27" colspan=2></TD> <td><B>Total Time:</B></TD><td bgcolor=#FFFF99><B>$all_time</B></TD><t +d BGCOLOR="#eecb27"></TD></TR></TABLE></CENTER> <P> <HR WIDTH=80%> </BODY></HTML> HTML } # End of normal_results subroutine ################ END NORMAL RESULTS SUBROUTINE

Replies are listed 'Best First'.
Re: Time Totaling Lost
by dws (Chancellor) on Nov 24, 2001 at 07:21 UTC
    In the process of converting my script to run under strict I lost one of my most important operations. The variable $all_time is suposed to be a total of all times in my array.

    In normal_results, you're starting over with $total_time each time you fetch a row. Move the declaration of $total_time from inside to while loop, and put it at the top of the subroutine. Then it will accumulate time, rather than getting reset each time the loop is entered.

      Worked like a charm, Thanks a million. Do you take personal checks?

      Glad to help. I don't take checks, but the Offering Plate can always use a donation.

        It's christmas time....almost. This is one of the few websites that I have used and kept using that I definatly think is donation worthy. Thanks, all in the Monastery.
      Worked like a charm, Thanks a million. Do you take personal checks? hehe. I'm still learning the mechanics of perl (physics?) Sometimes I overlook things.