in reply to Re^11: Computing results through Arrays
in thread Computing results through Arrays

Sorry I was not clear with my requirement, also I made worst mistake in my code.

My requirement is to print the Maximum value the same way Average value printing works. So the assignment of values to hash array keys should be max value instead of sum of the values in that group.

while(<DATA>){ next unless /\w/; my($server,$datetime,$database,$speed) = (split)[0,1,2,3]; my $ddhhmm = substr $datetime,0,16; my $ddhh = substr $datetime,0,13; $h{$ddhh }{$database} += $speed; $m{$ddhhmm}{$database} += $speed; $db{$database} = 1; $sr{$server } = 1; }

Is there a way to assign maximum value in this while loop shown above to $h{$ddhh }{$database} and $m{$ddhhmm}{$database} ??

If this is possible then I can follow the same procedure to print Max values the same way we did for Average.

One more doubt... All I did to find Average is to loop through the Hash Array one more time and assign the Average value to it as shown below

for my $key (sort keys %h){ for (@db) { $h{$key}{$_} = round($h{$key}{$_} / ($count * 60))} ; }

Can it be done efficiently without looping the Hash Array again ? I mean can this be done in while loop itself ?

Replies are listed 'Best First'.
Re^13: Computing results through Arrays
by aaron_baugher (Curate) on Jun 24, 2015 at 23:07 UTC

    I may still not understand exactly what you're trying to do. But if you want to keep track of a max value for each time/database at the same time that you're keeping a total for the purpose of averaging, you could do something like this inside your while() loop:

    $h{$ddhh }{$database}{total} += $speed; $m{$ddhhmm}{$database}{total} += $speed; $h{$ddhh }{$database}{max} = max(($h{$ddmm}{$database}{max} || 0), + $speed); $m{$ddhhmm}{$database}{max} = max(($h{$ddmm}{$database}{max} || 0), + $speed);

    You can use the max() function from List::Util or write your own. Now later in the code where you used to access the total speed with $h{$timestamp}{$database}, you'll change that to access it as $h{$timestamp}{$database}{total}. And that makes room to keep track of the maximum value for each one in $h{$timestamp}{$database}{max}. Make sense?

    Aaron B.
    Available for small or large Perl jobs and *nix system administration; see my home node.

      Thanks, this is what exactly I was looking out for. Once again thanks a lot.