Greetings suggus,

Try this...

open(HANDLE,"script_integration_records"); while (<HANDLE>) { @temp1 = split(/:/); $domain = unpack ("A4", $temp1[40]); if (($domain eq "ATM") && ($temp1[36] eq "In Production")) { $sum += $temp1[38]; $count++; } } close(HANDLE); $average = $sum / $count; print $average, "\n";

-gryphon
code('Perl') || die;

UPDATE:

Well, here's a smaller version of the above. I'm sure I could use map to get this in a line or two less, but I'm still having difficulties really getting my mind around complicated multi-calls of map. (Anyone out there know a tutorial on map?)

open(HANDLE, 'script_integration_records'); foreach (grep /^(?:[^:]*:){36}In Production:/, <HANDLE>) { my @line = split(/:/); push @numbers, $line[38] if (unpack("A4", $line[40]) eq "ATM"); } close(HANDLE); print eval(join('+',@numbers))/($#numbers+1), "\n";

Theoretically, you could run multiple greps against eachother to get just a single array. Then just average the array. Also, I have a general "ungood" feeling about using eval anywhere for any reason. Perhaps it's just a personal problem, but I have this fear of stuff being executed without my pre-knowledge of its contents.

-gryphon
code('Perl') || die;


In reply to Re: Calculating the average of a column in a flat-file database by gryphon
in thread Calculating the average of a column in a flat-file database by suggus

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.