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

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);

Replies are listed 'Best First'.
Re^5: seeking advise on average value problem
by graff (Chancellor) on Mar 26, 2006 at 01:47 UTC
    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.