in reply to Re: Making a two dimensional hash with first row and column as the keys
in thread Making a two dimensional hash with first row and column as the keys
A slight variation on toolic's approach:
perl -wMstrict -le "my $data = qq{Log foo bar baz \n} . qq{aaa 123 456 789 \n} . qq{bbb 987 654 321 \n} ; ;; open my $fh, '<', \$data or die qq{opening data: $!}; ;; my (undef, @dim2_names) = split /\s+/, <$fh>; print qq{ @dim2_names}; ;; my %hash_2d; while (defined(my $rec = <$fh>)) { my ($row, @vals) = split /\s+/, $rec; print qq{$row = @vals}; @{ $hash_2d{$row} }{ @dim2_names } = @vals; } ;; use Data::Dumper; print Dumper \%hash_2d; " foo bar baz aaa = 123 456 789 bbb = 987 654 321 $VAR1 = { 'bbb' => { 'bar' => '654', 'baz' => '321', 'foo' => '987' }, 'aaa' => { 'bar' => '456', 'baz' => '789', 'foo' => '123' } };
|
|---|