Perhaps the following data structure--a hash of hashes of arrays (HoHoA)--would be helpful (as suggested in your posting's topic):

use strict; use warnings; use Data::Dumper; my %chrHash; push @{ $chrHash{"chr1"}{3} }, 100; push @{ $chrHash{"chr1"}{3} }, 300; print 'Count for chr1 3: ', scalar @{ $chrHash{"chr1"}{3} }, "\n"; print qq/Locations for chr1 3: @{ $chrHash{"chr1"}{3} }\n\n/; print Dumper \%chrHash;

Output:

Count for chr1 3: 2 Locations for chr1 3: 100 300 $VAR1 = { 'chr1' => { '3' => [ 100, 300 ] } };

The arrays would track both count and locations.

On another issue, consider:

$chrHash{$_} = { %countHash } for @chromosomes;

instead of using map in a void context, like:

map { $chrHash{$_} = { %countHash } } @chromosomes;

Also, consider using grep or List::Util's first instead of the smart-match operator (see Smart matching is experimental/depreciated in 5.18 - recommendations?):

use strict; use warnings; use List::Util qw/first/; my @chromosomes = ( "chr1", "chr2", "chr3", "chr5" ); my @line = ( "chr1", "chr3", "chr5" ); if ( grep $line[2] eq $_, @chromosomes ) { print "Found $line[2] using grep!\n"; } if ( first { $line[2] eq $_ } @chromosomes ) { print "Found $line[2] using first!\n"; }

Output:

Found chr5 using grep! Found chr5 using first!

The advantage of first is that it terminates upon the first successful find, whereas grep will iterate through the entire list.


In reply to Re: Automatic creation of a hash of hashes of arrays (?) by Kenosis
in thread Automatic creation of a hash of hashes of arrays (?) by bontus

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.