maderman has asked for the wisdom of the Perl Monks concerning the following question:
and for categories cat1..cat15, I'd like to get an average for each row AND an average for each column. I'm having problems trying to work out how to get the latter. Here is what I have so far:report_no, reported_by project_no date_reported cat1 cat2 cat3 cat4 cat5 cat6 cat7 cat8 cat9 cat10 cat11 cat12 cat13 cat14 cat15
Can anyone help me with getting averages for each column? I'm all hashed out...use strict; $sth = $dbh->prepare(qq{ SELECT report_no, reported_by, project_no, to_char(date_reported,'DD-MON-YYYY'), cat1 . . . cat15 FROM TABLE ORDER BY report_no DESC }); $sth->execute(); $array_ref = $sth->fetchall_arrayref(); $i = 0; foreach $row (@$array_ref) { ($report_no,$reported_by,$project_no,$date_reported) = @$row[0..3]; #Categories in each row may have undef elements, replace #with a '-'. For defined values, sum them up. This sum is #used to calculate the average for each row. $number = 0; $sum = 0; foreach $element (@$row[4..18]) { if ($element == -1) { $element = '-' } else { $element = sprintf "%.2f", $element; $sum += $element; ++$number; } } $row_avge = sprintf "%.2f", $sum / $number; $i++; } $sth->finish(); $dbh->disconnect;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBI and fetchall_arrayref (gosh!)
by busunsl (Vicar) on Nov 16, 2001 at 13:48 UTC | |
|
Re: DBI and fetchall_arrayref (gosh!)
by Masem (Monsignor) on Nov 16, 2001 at 16:55 UTC | |
by maderman (Beadle) on Nov 16, 2001 at 17:31 UTC | |
|
Re: DBI and fetchall_arrayref (gosh!)
by runrig (Abbot) on Nov 16, 2001 at 22:31 UTC |