in reply to Calculating the average of a column in a flat-file database

Instead of printing, you can do a summing calculation to determine the total, then average over those.
my ( $total , $number ); if ($domain eq "ATM") { if ($temp1[36] eq "In Production") { $total += $temp1[38]; $number++; } } } my $average = ( $number ) ? $total/$number : 0 ; #catch in case of no +data

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important

Replies are listed 'Best First'.
Re: Re: Calculating the average of a column in a flat-file database
by suggus (Sexton) on Oct 26, 2001 at 02:47 UTC
    Masem,

    But my main goal is to display the average of that calculation...BTW I'm new to perl.

    I have a hard time with loops and I just wanted the script to look through the DB and find the instances of "In Production". With the instances found, I would like to find the average of the values in my array $temp1[38] for the row that contained "In Production" and print out the average.
      But my main goal is to display the average of that calculation

      Well, that's not hard :) - actually more than trivial, you output values by printing them. So after Masem's code put either one of those

      # with string interpolation print "The average is $average.\n"; # or with string concat print "The average is " . $average . ".\n"; # or with multiple arguments to print print "The average is ", $average, ".\n"; # or using printf and rounding to 2 decimals printf "The average is %.2f.\n", $average;

      I suggest you try to get a good book on perl, e.g. Learning Perl (3rd edition) by our one and only merlyn. Also have a look onto our New Monks Info Page. There you will find lot's of sources of information on perl and tips to get started.

      -- Hofmator