G'day ila14,
Welcome to the monastery.
My biggest problem with this is determining the structure of the multidimensional hash you're trying to create.
Your description, "I would like to hash: % hash1 = Column1 => Column 2. %hash2 = %hash1 => Column3. %hash3 = %hash2 => Column 4.", conveys no real meaning. Your code is not helpful either: given you've stated "the syntax is not correct", this isn't too surprising.
From the code you've posted, I suspect you'd benefit from reading "perlintro -- a brief introduction and overview of Perl".
For information on data structures, I suggest you read "perldsc - Perl Data Structures Cookbook"; paying particular attention to the "HASHES OF HASHES" section.
The actual code you need may be as simple as this:
#!/usr/bin/env perl use strict; use warnings; use autodie; use constant { ID => 0, SYMBOL => 1, 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[GO_ID]} = $cols[GO_NAME]; } use Data::Dump; dd \%go_hash;
The file pm_1076856.tsv contains the input data you posted. Here's the output after running my example script:
{ Symbol1 => { H1SXX9 => { "GO:0015031" => "protein transport" } }, Symbol2 => { H1SXZ5 => { "GO:0003824" => "catalytic activity", "GO:0008152" => "metabolic process", "GO:0008728" => "GTP diphosphokinase activity", "GO:0015969" => "guanosine tetraphosphate metabolic p +rocess", "GO:0016301" => "kinase activity", "GO:0016310" => "phosphorylation", "GO:0016597" => "amino acid binding", "GO:0016740" => "transferase activity", }, }, Symbol3 => { H1SXZ8 => { "GO:0006812" => "cation transport", "GO:0008324" => "cation transmembrane transporter act +ivity", "GO:0030001" => "metal ion transport", "GO:0046872" => "metal ion binding", "GO:0055085" => "transmembrane transport", }, }, Symbol4 => { H1SY02 => { "GO:0006810" => "transport", "GO:0008565" => "protein transporter activity", "GO:0015031" => "protein transport", }, }, Symbol5 => { H1SY06 => { "GO:0004129" => "cytochrome-c oxidase activity", "GO:0005506" => "iron ion binding", }, }, }
If that's close to what you want, try changing the hash depth and @cols indices to get whatever you require.
If that's completely different from what you're after, and you still can't work out what code you need, reduce your example data to a more manageable size for demonstration purposes (maybe half a dozen records) and post the actual data structure you require (something along the lines of my posted output would be preferable).
Also take a look at the guidelines in "How do I post a question effectively?" for hints and tips on what you can do to help us to help you.
-- Ken
In reply to Re: Multidimensional hash help!
by kcott
in thread Multidimensional hash help!
by ila14
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |