Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have this file of stats:
"\\TEST\Memory\Pages/sec" "\\TEST\PhysicalDisk" "06/20/2002 08:27:19.087" "0.87367121907974665" "06/20/2002 08:27:34.088" "3.3339663084377777" "06/20/2002 08:27:49.090" "3.9996207699833071" "06/20/2002 08:28:04.091" "3.198195381472539"
Now I have this code to grab the data, and I now want to calculate the averages on each column. Is there an easy way to do that dynamically counting the rows then obviously I would add them and calculate the average:
#!C:\Perl\bin\perl use Getopt::Std; $USAGE = <<USAGE; Usage: Absolute_Path\\Logfile A script to retrieve perf numbers and calculate averages USAGE ; # process command line getopts('x') || die $USAGE; # get parameters $Perf = shift || die $USAGE; chomp ($Perf); open (LOG, "$Perf") || die "Error: Couldn't open $Perf : $!\n"; @read = <LOG>; print "Contents of read are: @read\n";

Replies are listed 'Best First'.
Re: Formating Text Files
by hotshot (Prior) on Jun 20, 2002 at 15:35 UTC
    If I undestood you right, then you can try something like that:
    $i = 0, $total = 0; while ($line = <FILE>) { chomp $line; if ($line =~ /"([\d\/]+)\s([\d:.]+)"\s+"([\d.]+)"/) { total += $3; $i++; } } $avarage = $total/$i;
    This will give you only the avarage of the number in the right column, but you can easily do that for the time and date (write a timeAvarage function), I'll leave that for you.

    Thanks.

    Hotshot
Re: Formating Text Files
by robobunny (Friar) on Jun 20, 2002 at 15:48 UTC
    if your data file is large, you might see some performance benefit from using a split instead of a more complicated regex:
    # skip the header line, assuming it's the first line # otherwise, you'll have to check in the loop to see # if you have grabbed a number or a string my $header = <FILE>; my $total = 0; my $count = 0; while(<FILE>) { $total += (split('"', $_))[3]; $count++; } print "Average: ",$total/$count, "\n";
    if you have multiple header lines inside the file, you will need to check in the loop to make sure that you skip the header lines.
      Thanks, good ideas! I was taking the 'scenic route' as I usually do. and making things to complicated.
Re: Formating Text Files
by DamnDirtyApe (Curate) on Jun 20, 2002 at 15:38 UTC

    Assuming that $Perf in the data file in your example, you can just read @read in scalar context to find the number of elements.

    $lines_in_file = @read;

    Update: Forgot about counting the header row (thanks bronto). Read below.


    _______________
    D a m n D i r t y A p e
    Home Node | Email
      ...minus one (the heading row). So, actually, he could use $#read directly, or --$lines_in_file ;