Hi all. With DBI and Oracle I'm working with a database with the following entries (cat1..cat15 are categories):
report_no, reported_by project_no date_reported cat1 cat2 cat3 cat4 cat5 cat6 cat7 cat8 cat9 cat10 cat11 cat12 cat13 cat14 cat15
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:
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;
Can anyone help me with getting averages for each column? I'm all hashed out...
Many thanks in advance.
Stacy.

In reply to DBI and fetchall_arrayref (gosh!) by maderman

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.