in reply to Re: seeking advise on average value problem
in thread seeking advise on average value problem

this was more what I had in mind. Basically I have a log from another script that I would like to calculate different. It produces a couple differnt averages each hour on each day but they are all mixed and I wanted a way to sort out the mess of 5 weeks worth of data rather than go back to excel.
  • Comment on Re^2: seeking advise on average value problem

Replies are listed 'Best First'.
Re^3: seeking advise on average value problem
by rangersfan (Novice) on Mar 26, 2006 at 00:52 UTC
    what if I wanted to go a step further, and say that i have

    joe_average: 2.2 jack_average: 3.2 stan_average: 9.1 <p> next day joe_average: 5.2 jack_average: 4.2 stan_average: 6.1 <p> next day joe_average: 3.2 jack_average: 4.2 stan_average: 5.1
    How can I get somthing like this to work....

    my %stats; while (<FILE>) { next unless /^(jack_average)\w+:\s+(\d+\.\d+)$/; $stats{$1}{count}++; $stats{$1}{sum} += $2; } printf( "%20s %5s %8s\n", "Name", "Count", "Average" ); for ( sort keys %stats ) { printf( "%19s: %5d %8.2f\n", $_, $stats{$_}{count}, $stats{$_}{sum} / $stats{$_}{count} ); }
      I got it..... least it works, who knows if its the best method.
      #!/usr/bin/perl open( FILE, "average.txt") or die "Can't open average.txt : $!"; while (<FILE>) { next unless (/^(joe_average):\s+(\d+\.\d+)/) ; $stats{$1}{count}++; $stats{$1}{sum} += $2; } printf( "%20s %5s %8s\n", "Name", "Count", "Average" ); for ( sort keys %stats ) { printf( "%19s: %5d %8.2f\n", $_, $stats{$_}{count}, $stats{$_}{sum} / $stats{$_}{count} ); } close(FILE);
        That's fine if the only information you want to track is the input involving "joe_average". In fact, if you only want the data for this one name, and everything else can be ignored, you don't need the HoH -- just use one scalar to hold the count, and one to hold the sum.

        But if you put "\w+" instead of the explicit string "joe_average" into the regex match inside the while loop, your one hash will collect the numbers for all the different names, and you can use them all or just the ones you are interested in.

      This is the form that I ended using in the end. In addition to the average I also had values that needed to be totaled. One of the problems I ran into was if I didn't close the file before I started the new hash it wouldn't pick any values. I'm not sure if thats a to be expected part or inexperience. thanks all.
      #!/usr/bin/perl open( FILE, "process.log") or die "Can't open average.txt : $!"; while (<FILE>) { next unless ( /(\w+)_mean_time:\s+([\d.]+)/ ); $stats{$1}{count}++; $stats{$1}{sum} += $2; } printf( "%20s %5s %8s\n", "Name", "hours", "Average" ); for ( sort keys %stats ) { printf( "%19s: %5d %8.2f\n", $_, $stats{$_}{count}, $stats{$_}{sum} / $stats{$_}{count} ); } close(FILE); open( FILE, "process.log") or die "Can't open average.txt : $!"; while (<FILE>) { next unless ( /(\w+)_total_counts:\s+(\d+)/ ); $counter{$1}{count}++; $counter{$1}{sum} += $2 } printf( "%20s %5s %8s\n", "Name", "hours", "total" ); for ( sort keys %counter ) { printf( "%19s: %5d %8.2f\n", $_, $counter{$_}{count}, $counter{$_}{sum} ); } close(FILE);