foreach $filiais (@filiais){
# print an html header (you don't need $cnt2 for this)
$tf = $trf = $tm = $trm = 0; # set accumulators to 0
foreach $month (1 .. 12) {
# query DB for rows of 4 values _for_this_month_
while (@rown = $sthn->fetchrow_array()) {
($fa,$rfa,$ma,$rma) = @rown;
$tf += $fa; # and likewise for the other three accumulators
}
if ( $month % 3 == 0 ) { # true for months 3,6,9,12
ListTrimestre( $tf,$trf,$tm,$trm,$month/3 );
$tf = $trf = $tm = $trm = 0; # reset accumulators to 0
}
}
}