Often, depending upon other usage, it makes sense to avoid deeply nested hashes by combining the keys into a single key.

Ie: instead of:

my %snp; for my $snpId ( ... ) { for my $geneId ( ... ) { for my $transcriptId ( ... ) { for my $propertyId ( ... ) { $snp{ $snpId }{ $geneId }{ $TranscriptId }{ $propertyI +d } = ...; } } } }

Use:

my %snp; for my $snpId ( ... ) { for my $geneId ( ... ) { for my $transcriptId ( ... ) { for my $propertyId ( ... ) { $snp{ join $;, $snpId, $geneId, $TranscriptId, $proper +tyId } = ...; } } } }

The result is a single level hash with very similar selection properties to the multilevel hash, that uses far less memory, is faster to access, and far, far easier to compare one with another.

$; is a control character--ascii 28--which is unlikely to appear in any normal text hash key, is used to join the keys together to form a composite key. In the unlikely event that there is the possibility of your Ids containing that character, then there are other characters that can be used to delimit the join that may be preferable. Eg, the null character (chr(0)) or chr(255) (sometimes labelled DEL), which may be better candidates. If your ids can be unicode, you may need to be more selective.


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.

In reply to Re: can i avoid all these nested hashes by BrowserUk
in thread can i avoid all these nested hashes by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.