In the quest to make my data more accessible, I want to store my tabulated data in a complex hash. I am trying to grow a \'HoHoHoA\' as the script loops over my data. As per
This should do it for you.
#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw/sum/;
sub avg { sum(@_) / @_ }
my $fileName = shift;
open my $fh, "<", $fileName
or die "Unable to open $fileName: $!\n";
my %monthData;
chomp(my @headers = split /\t+/, <$fh>);
while (<$fh>) {
chomp;
my %rec;
@rec{@headers} = split /\t+/;
my ($hour) = split /:/, $rec{TIME}, 2;
for my $key (grep { not /^(DATE|TIME)$/ } keys %rec) {
push @{ $monthData{$key}{$rec{DATE}}{$hour} }, $rec{$key};
}
}
for my $column (keys %monthData) {
for my $date (keys %{ $monthData{$column} }) {
for my $hour (keys %{ $monthData{$column}{$date} }) {
my $avg = avg @{ $monthData{$column}{$date}{$hour} };
print "average of $column for $date $hour is $avg\n";
}
}
}
Things to pay attention to: