Perl: Generating Arrays inside a Complex Hash

后端 未结 3 1623
臣服心动
臣服心动 2021-01-16 09:54

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

3条回答
  •  北海茫月
    2021-01-16 10:40

    I hope the following program populates the data structure you want:

    #!/usr/bin/perl                        
    
    use strict;
    use warnings;
    use Data::Dumper;
    
    open my $fh, '<', 'input' or die $!;
    
    my @headers;
    for ( split /\t/, ~~ <$fh> ) {
        chomp;
        push @headers, $_ unless /^\t?$/;
    }
    
    my %monthData;
    while (<$fh>) {
        my @line;
        for ( split /\t/ ) {
            chomp;
            push @line, $_ unless /^\t?$/;
        }
    
        for my $i ( 2 .. $#headers ) {
            my ($hour) = split /:/, $line[1];
            push @{ $monthData{ $headers[$i] }->{ $line[0] }->{$hour} }, $line[$i];
        }
    }
    
    print Dumper \%monthData;
    

提交回复
热议问题