in reply to Re^2: Multidimensional hash help!
in thread Multidimensional hash help!

Thanks for fixing the formatting; however, instead of creating a new post you can just edit the original (see "How do I change/delete my post?"). Don't worry about the original (Re^2: Multidimensional hash help!): I've requested that it be reaped.

When you post, please be specific about what you're doing. In this case, I've guessed $symb refers to the Symbol column. $UniID and $TaX are unclear (there's two columns with ID and two with Taxon): UniID and TaX may be standard abbreviations where you work (or generally in your industry) but I don't work in your industry nor do most of the people here who could help you.

The number of levels of the hash shouldn't be an issue: I'm guessing another nested for loop would've accessed all the data. In the script below, I've added another level (to what I had in my previous script) and shown how to print the fields. [For future reference, you'll find logically indenting your code makes it a lot easier to read and maintain (compare your code with mine).]

Data::Dump (which I used in my previous script) is a CPAN module which you may need to install. Data::Dumper is a built-in module. I've shown usage examples of both for comparison — you'll need click on "Reveal this spoiler" to see the output.

#!/usr/bin/env perl use strict; use warnings; use autodie; use constant { ID => 0, SYMBOL => 1, TAXON_NAME => 3, GO_ID => 5, GO_NAME => 6, }; my $file = './pm_1076856.tsv'; my %go_hash; open my $fh, '<', $file; while (<$fh>) { next if $. == 1; my @cols = split /\t/; $go_hash{$cols[SYMBOL]}{$cols[ID]}{$cols[TAXON_NAME]}{$cols[GO_ID] +} = $cols[GO_NAME]; } close $fh; for my $symbol (sort keys %go_hash) { for my $id (sort keys %{$go_hash{$symbol}}) { for my $taxon_name (sort keys %{$go_hash{$symbol}{$id}}) { for my $go_id (sort keys %{$go_hash{$symbol}{$id}{$taxon_n +ame}}) { print join("\t" => $symbol, $id, $taxon_name, $go_id, $go_hash{$symbol}{$id}{$taxon_name +}{$go_id} ), "\n"; } } } } { print "\nData::Dumper Output:\n"; use Data::Dumper; local $Data::Dumper::Indent = 1; print Dumper \%go_hash; } print "\nData::Dump Output:\n"; use Data::Dump; dd \%go_hash;

Output:

Symbol1 H1SXX9 Homo Sapiens GO:0015031 protein transport Symbol2 H1SXZ5 Homo Sapiens GO:0003824 catalytic activity Symbol2 H1SXZ5 Homo Sapiens GO:0008152 metabolic process Symbol2 H1SXZ5 Homo Sapiens GO:0008728 GTP diphosphokinase + activity Symbol2 H1SXZ5 Homo Sapiens GO:0015969 guanosine tetraphos +phate metabolic process Symbol2 H1SXZ5 Homo Sapiens GO:0016301 kinase activity Symbol2 H1SXZ5 Homo Sapiens GO:0016310 phosphorylation Symbol2 H1SXZ5 Homo Sapiens GO:0016597 amino acid binding Symbol2 H1SXZ5 Homo Sapiens GO:0016740 transferase activit +y Symbol3 H1SXZ8 Homo Sapiens GO:0006812 cation transport Symbol3 H1SXZ8 Homo Sapiens GO:0008324 cation transmembran +e transporter activity Symbol3 H1SXZ8 Homo Sapiens GO:0030001 metal ion transport Symbol3 H1SXZ8 Homo Sapiens GO:0046872 metal ion binding Symbol3 H1SXZ8 Homo Sapiens GO:0055085 transmembrane trans +port Symbol4 H1SY02 Homo Sapiens GO:0006810 transport Symbol4 H1SY02 Homo Sapiens GO:0008565 protein transporter + activity Symbol4 H1SY02 Homo Sapiens GO:0015031 protein transport Symbol5 H1SY06 Homo Sapiens GO:0004129 cytochrome-c oxidas +e activity Symbol5 H1SY06 Homo Sapiens GO:0005506 iron ion binding

Data::Dumper Output: $VAR1 = { 'Symbol4' => { 'H1SY02' => { 'Homo Sapiens' => { 'GO:0008565' => 'protein transporter activity', 'GO:0015031' => 'protein transport', 'GO:0006810' => 'transport' } } }, 'Symbol3' => { 'H1SXZ8' => { 'Homo Sapiens' => { 'GO:0008324' => 'cation transmembrane transporter activity', 'GO:0046872' => 'metal ion binding', 'GO:0006812' => 'cation transport', 'GO:0030001' => 'metal ion transport', 'GO:0055085' => 'transmembrane transport' } } }, 'Symbol1' => { 'H1SXX9' => { 'Homo Sapiens' => { 'GO:0015031' => 'protein transport' } } }, 'Symbol5' => { 'H1SY06' => { 'Homo Sapiens' => { 'GO:0005506' => 'iron ion binding', 'GO:0004129' => 'cytochrome-c oxidase activity' } } }, 'Symbol2' => { 'H1SXZ5' => { 'Homo Sapiens' => { 'GO:0016597' => 'amino acid binding', 'GO:0015969' => 'guanosine tetraphosphate metabolic process', 'GO:0016740' => 'transferase activity', 'GO:0003824' => 'catalytic activity', 'GO:0016301' => 'kinase activity', 'GO:0008152' => 'metabolic process', 'GO:0016310' => 'phosphorylation', 'GO:0008728' => 'GTP diphosphokinase activity' } } } }; Data::Dump Output: { Symbol1 => { H1SXX9 => { "Homo Sapiens" => { "GO:0015031" => "protei +n transport" } }, }, Symbol2 => { H1SXZ5 => { "Homo Sapiens" => { "GO:0003824" => "catalytic activity", "GO:0008152" => "metabolic process", "GO:0008728" => "GTP diphosphokinase activity", "GO:0015969" => "guanosine tetraphosphate metabolic + process", "GO:0016301" => "kinase activity", "GO:0016310" => "phosphorylation", "GO:0016597" => "amino acid binding", "GO:0016740" => "transferase activity", }, }, }, Symbol3 => { H1SXZ8 => { "Homo Sapiens" => { "GO:0006812" => "cation transport", "GO:0008324" => "cation transmembrane transporter a +ctivity", "GO:0030001" => "metal ion transport", "GO:0046872" => "metal ion binding", "GO:0055085" => "transmembrane transport", }, }, }, Symbol4 => { H1SY02 => { "Homo Sapiens" => { "GO:0006810" => "transport", "GO:0008565" => "protein transporter activity", "GO:0015031" => "protein transport", }, }, }, Symbol5 => { H1SY06 => { "Homo Sapiens" => { "GO:0004129" => "cytochrome-c oxidase activity", "GO:0005506" => "iron ion binding", }, }, }, }

-- Ken