in reply to printing outside a for loop

You need to calculate the average outside the foreach loop.

use warnings; use strict; # sub get_avr { # 'get' implies that the sub is returning a value? sub print_avr { my ( $cluster ) = @_ # open(IN, "$outputfile"); # Where does $outputfile come from? You should pass this value # to the subroutine instead of using globals. What happens when # you try to read from an invalid filehandle? open IN, '<', $outputfile or die "Cannot open '$outputfile' $!"; my %data; while ( <IN> ) { my ( $hour, $cpu ) = /(\d+):\d+\s+([\d.]+)/ or next; $data{ hour }{ total } += $cpu; $data{ hour }{ count }++; } close IN; for my $hour ( sort { $a <=> $b } keys %data ) { print "$cluster $hour:00 ", $data{ $hour }{ total } / $data{ $ +hour }{ count }, "\n"; }