in reply to Average calculation

Your data looks like it warrants a hash of hash rather than one-level hash structure; in other words it suggests something like:
my %HoH = (); # 'my' declares at file scope in this case LoadFile(\%HoH); # passing by reference is more portable # if you later make a module Stats(\%HoH); sub LoadFile{ my $href = shift; # get the actual parameter open my $fileHandle, "<filepath" or die $!; while( <$fileHandle> { my ($st,$to,$te) = split( /\:/ ); $href -> { $st }{ $to } = $te; # load file data into the HoH } close $fileHandle; } sub Stats { my $href = shift; for my $st ( sort keys %$href ) { # in state order my $toref = $href -> { $st }; # ref. to town subhash my ( $count, $sum ) = (0,0); for my $to ( keys %$toref ) { # sum through towns # (any order will do) $count++; $sum += ( $toref -> { $to } ); # accumulate state aggregates } print "$st: " . $sum/$count . "\n"; # average for state = sum/count } }

-M

Free your mind