Greetings,fellas.

I'm in deep need of enlightening here. I'm writing a report script that should generate values monthly and then bring totals by trimesters.

The thing is I'm doing something wrong on the loops that is adding more to each trimester's total that it should. I've tried many things to solve it, but I'm failing and failing.

What I need here is more a theorical than practical explanation, I think.

Sorry by the large piece of code, but I think it is vital to understand the whole thing.
Edited by boo_radley : READMORE added.


Here's it:
sub acumulado_f #I've called a sub 'cause there's more than one type o +f report { $titulo = "Report by state"; $dbh = SysUtils::Connect; $sthf = $dbh->prepare_cached("SELECT uf FROM filiais ORDER BY uf") +; #selects each state $xf = 0; $sthf->execute; while (@dataf = $sthf->fetchrow_array()) { $filiais[$xf] = $dataf[0]; $xf++; } foreach $filiais(@filiais)#foreach state { $cnt =0; $cnt2 = 0; if ($cnt2 eq 0)# if it's the first result, assemble a header { $message .= "<BR><TABLE WIDTH=\"100%\" BORDER=1> <TR class=\"texto3\"> <TD colspan=\"9\" BGCOLOR=\"#FFFFFF\"><span class =\"texto +2\">State: $filiais</span></TD> </TR> <TR> <TD WIDTH=\"10%\">&nbsp;</TD> <TD COLSPAN=\"4\" class=\"texto2\" align=\"center\">Incom +e</TD> <TD COLSPAN=\"4\" class=\"texto2\" align=\"center\">Margi +n</TD> </TR> <TR> <TD WIDTH=\"10%\" class=\"texto2\">&nbsp;</TD> <TD WIDTH=\"13%\" ALIGN=\"CENTER\" class=\"texto2\">Predic +ted</TD> <TD WIDTH=\"13%\" ALIGN=\"CENTER\" class=\"texto2\">Realiz +ed</TD> <TD WIDTH=\"5%\" ALIGN=\"CENTER\" class=\"texto2\">%</TD> <TD WIDTH=\"13%\" ALIGN=\"CENTER\" class=\"texto2\">Differ +ence</TD> <TD WIDTH=\"13%\" ALIGN=\"CENTER\" class=\"texto2\">Predic +ted</TD> <TD WIDTH=\"13%\" ALIGN=\"CENTER\" class=\"texto2\">Realiz +ed</TD> <TD WIDTH=\"5%\" ALIGN=\"CENTER\" class=\"texto2\">%</TD> <TD WIDTH=\"14%\" ALIGN=\"CENTER\" class=\"texto2\">Differ +ence</TD> </TR>"; $cnt2++; } for ($m=1; $m<=12; $m++)#Make an array of months { $months[$m] = $m; } foreach $months(@months) { if ($months ne "")#escapes the obligatory 0 position of th +e array - blame me later for this one :) { $sh_mes = SysUtils::Extenso_Mes("$months");#Return the + month name $fat = 0; $mar = 0; # In the database there are fields with the same number as months: $fa = "A.f".$months; $ma = "A.m".$months; $rf = "A.rf".$months; $rm = "A.rm".$months; $tf = 0; # First predicted value $trf = 0;# First realized value $tm = 0;# Second Predicted value $trm = 0;# Second realized value $ctp = 0; $sthn = $dbh->prepare_cached("SELECT DISTINCT ".$fa." +, ".$ma." , ".$rf." , ".$rm." FROM metas A, usuarios B WHERE B.filial +='$filiais' AND A.login=B.login") or die "Preparing error. ".$dbh->er +rstr; $sthn->execute or die "executing error. ".$sthn->errst +r; #This statement works fine. I've debugged it and tested it while (@rown = $sthn->fetchrow_array()) { $fat = $rown[0]; $mar = $rown[1]; $rfat = $rown[2]; $rmar = $rown[3]; $tf += $fat; #First predicted value $tm += $mar; #Second predicted value $trf += $rfat;#First realized value $trm += $rmar;#Second realized value $ctp++; if ($months eq "1") #January, obviously { $tft = 0; #trimester predicted 1st total $trft = 0; #trimester realized 1st total $tmt = 0; #trimester predicted 2nd total $trmt = 0; #trimester realized 2nd total $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } elsif ($months eq "2" or $months eq "3") { $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } elsif ($months eq "4") { $tft = 0; $trft = 0; $tmt = 0; $trmt = 0; $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } elsif ($months eq "5" or $months eq "6") { $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } elsif ($months eq "7") { $tft = 0; $trft = 0; $tmt = 0; $trmt = 0; $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } elsif ($months eq "8" or $months eq "9") { $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } elsif ($months eq "10") { $tft = 0; $trft = 0; $tmt = 0; $trmt = 0; $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } elsif ($months eq "11" or $months eq "12") { $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } } $sthn->finish; if ($ctp ne 0) { ListaDados(); #brings value for each month if ($months eq "3")# Is it a trimester? { $trim = 1; ListaTrimestre(); #bring values (totals) for e +ach trimester } elsif ($months eq "6") { $trim = 2; ListaTrimestre(); } elsif ($months eq "9") { $trim = 3; ListaTrimestre(); } elsif ($months eq "12") { $trim = 4; ListaTrimestre(); } } else { if ($cnt eq 0)#No values to show { $message .= "<TR CLASS=\"texto3\"> <TD BGCOLOR=\"$color2\" colspan=\"9\">State ha +s no values.</TD> </TR>"; } $cnt++; } } } } $dbh->disconnect; #open template to show values. print "Content-type: text/html\r\n\r\n"; open (FH,"templates/relatorios/relatorio_andamento.htm") or die "C +ant open body: $!"; $body = join('',<FH>); close (FH); eval "\$body = sprintf \"%s\",<<FYNYS;\n $body\nFYNYS\n"; print $body; }
The "ListaTrimestre" sub is a simple "put the vars into HTML code", so it displays later in the template. It is the $tft, $trft, $tmt and $trmt vars that are wrong. The problem is that I didn't found a pattern of error, so I acn't find where is the value that is being incorrectly added to the totals.

Can anyone point me the logic error here?

Thanks in advance,

Er Galvão Abbott
a.k.a. Lobo, DaWolf
Webdeveloper

In reply to Logic problem on loops by DaWolf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.