in reply to Help munging tabular data

Several structures are possible. I've used a HoHoA here. You might want to strip spaces from the hash keys if you have to make many references to them in your code.

#! perl -slw use strict; use Data::Dump qw[ pp ]; my %data; chomp( my @L1Headers = split "\t", scalar <DATA> ); chomp( my @L2Headers = split "\t", scalar <DATA> ); @data{ @L1Headers } = map{ +{ map{ $_ => undef } @L2Headers[ 0, 1 ] } } 1 .. @L1Headers; while( <DATA> ) { chomp; my @values = split "\t"; for my $l1 ( @L1Headers ) { for my $l2 ( keys %{ $data{ $l1 } } ) { push @{ $data{ $l1 }{ $l2 } }, shift @values; } } } print pp \%data; ## "What's Data Set 1's y value at Data Set 3's x3 value?" for ( 0 .. $#{ $data{ 'Data Set 1' }{ 'X units' } } ) { $data{ 'Data Set 3' }{ 'X units' }[ $_ ] eq 'x3' and print "DS3-X == x3, DS1-Y == ", $data{ 'Data Set 1' }{ 'Y units' }[ $_ ]; } __DATA__ Data Set 1 Data Set 2 Data Set 3 X units Y units X units Y units X units Y units x1 y1 x1 y1 x1 y1 x2 y2 x2 y2 x2 y2 x3 y3 x3 y3 x3 y3

Outputs

c:\test>junk2 { "Data Set 1" =>{ "X units" =>["x1","x2","x3"], "Y units" =>["y1","y2 +","y3"] }, "Data Set 2" =>{ "X units" =>["x1","x2","x3"], "Y units" =>["y1","y2 +","y3"] }, "Data Set 3" =>{ "X units" =>["x1","x2","x3"], "Y units" =>["y1","y2 +","y3"] }, } DS3-X == x3, DS1-Y == y3

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."