I need to create multi-dimensional hash.
for example I have done:
$hash{gene} = $mrna;
if (exists ($exon)){
$hash{gene}{$mrna} = $exon;
}
if (exists ($cds)){
$hash{gene}{$mrna} = $cds;
}
where $gene
, $mrna
, $exon
, $cds
are unique ids.
But, my issue is that I want some properties of $gene and $mrna to be included in the hash. for example:
$hash{$gene}{'start_loc'} = $start;
$hash{gene}{mrna}{'start_loc'} = $start;
etc. But, is that a feasible way of declaring a hash? If I call $hash{$gene}
both $mrna
and start_loc
will be printed. What could be the solution?
How would I add multiple values for the same key $gene and $mrna being the keys in this case.
Any suggestions will be appreciated.
You could try pushing each value onto a hash of arrays:
This way
gene
is the key, with multiple values ($mrna
,$exon
,$cds
) associated with it.Iterate over keys/values as follows:
The answer to a question I've asked previously might be of help (Can a hash key have multiple 'subvalues' in perl?).