为了使我的数据更易于访问,我想将我的表格数据存储在一个复杂的哈希中。当脚本循环遍历我的数据时,我正在尝试增加一个“HoHoHoA”。根据“perldsc”中的指南:
push @ { $hash{$column[$i]}{$date}{$hour} }, $data[$i];
该脚本编译并运行没有问题,但不向哈希添加任何数据:
print $hash{"Frequency Min"}{"09/07/08"}{"15"};
即使键应该存在,也不返回任何内容。在哈希上运行“存在”表明它不存在。
我正在阅读的数据文件如下所示:
DATE TIME COLUMN1 COLUMN2 COLUMN3...
09/06/2008 06:12:56 56.23 54.23 56.35...
09/06/2008 06:42:56 56.73 55.28 54.52...
09/06/2008 07:12:56 57.31 56.79 56.41...
09/06/2008 07:42:56 58.24 57.30 58.86...
.
.
.
我想将任何给定日期和时间的数组中每一列的值组合在一起,因此 {COLUMN}、{DATE} 和 {HOUR} 的三个哈希值。
生成的结构将如下所示:
%monthData = (
"COLUMN1" => {
"09/06/2008" => {
"06" => [56.23,56.73...],
"07" => [57.31,58.24...]
}
},
"COLUMN2" => {
"09/06/2008" => {
"06" => [54.23,55.28...],
"07" => [56.79,57.30...]
}
},
"COLUMN3" => {
"09/06/2008" => {
"06" => [56.35,54.52...],
"07" => [56.41,58.86...]
}
}
);
看看我的代码:
use feature 'switch';
open DATAFILE, "<", $fileName or die "Unable to open $fileName !\n";
my %monthData;
while ( my $line = <DATAFILE> ) {
chomp $line;
SCANROWS: given ($row) {
when (0) { # PROCESS HEADERS
@headers = split /\t\t|\t/, $line;
}
default {
@current = split /\t\t|\t/, $line;
my $date = $current[0];
my ($hour,$min,$sec) = split /:/, $current[1];
# TIMESTAMP FORMAT: dd/mm/yyyy\t\thh:mm:ss
SCANLINE: for my $i (2 .. $#headers) {
push @{ $monthData{$headers[$i]}{$date}{$hour} }, $current[$i];
}
}
}
}
close DATAFILE;
foreach (@{ $monthData{"Active Power N Avg"}{"09/07/08"}{"06"} }) {
$sum += $_;
$count++;
}
$avg = $sum/$count; # $sum and $count are not initialized to begin with.
print $avg; # hence $avg is also not defined.
Hope my need is clear enough. How can I append values to an array inside these sub-hashes?