in reply to seeking advise on average value problem

I think what you want is a hash-of-hashes data structure. The "outer" (main) hash would be keyed by the strings at the beginning of each line ("average1", "average2", etc). For each of those main hash elements, you have a sub-hash that stores the sum of values associated with the key string, and the number of times the key string occurred in the file.

Something like this would load that sort of hash, and then print summary values for each primary hash key:

my %stats; while (<>) { next unless ( /^(\w+):\s+([\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} ); }

Replies are listed 'Best First'.
Re^2: seeking advise on average value problem
by rangersfan (Novice) on Mar 26, 2006 at 00:21 UTC
    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.
      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);
        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);