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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |