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
Here's how I would write a program to do that.
#! /usr/bin/env perl
use strict;
use warnings;
use 5.010; # for say and m'(?)'
use YAML;
use Data::Dump 'dump';
my(%data,%original);
while( my $line = <> ){
next unless $line =~ m'
^ \s*
(? 0?[1-9] | [12][0-9] | 3[0-1] ) /
(? 0?[1-9] | 1[0-2] ) /
(? [0-9]{4} )
\s+
(? 0?[1-9] | 1[0-9] | 2[0-4] ) :
(? 0?[1-9] | [1-5][0-9] ) :
(? 0?[1-9] | [1-5][0-9] )
\s+
(? .* )
'x;
my @columns = split ' ', $+{columns};
push @{
$data{ $+{year} }
{ $+{month} }
{ $+{day} }
{ $+{hour} }
}, \@columns; # or [@columns]
# If you insist on having it in that data structure you can do this:
my $count = 1;
my $date = "$+{day}/$+{month}/$+{year}";
for my $column ( @columns ){
my $col = 'COLUMN'.$count++;
push @{ $original{$col}{$date}{$+{hour}} }, $column;
}
}
say Dump \%data, \%original; # YAML
say dump \%data, \%original; # Data::Dump
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
Either "perl program.pl datafile" or "perl program.pl < datafile"
---
2008:
06:
09:
06:
-
- 56.23
- 54.23
- 56.35
-
- 56.73
- 55.28
- 54.52
07:
-
- 57.31
- 56.79
- 56.41
-
- 58.24
- 57.30
- 58.86
---
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
(
{
2008 => {
"06" => {
"09" => {
"06" => [["56.23", "54.23", "56.35"], ["56.73", "55.28", "54.52"]],
"07" => [["57.31", "56.79", "56.41"], ["58.24", "57.30", "58.86"]],
},
},
},
},
{
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"] },
},
},
)