Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Automatic creation of a hash of hashes of arrays (?)

by Kenosis (Priest)
on Jan 27, 2014 at 18:21 UTC ( [id://1072262]=note: print w/replies, xml ) Need Help??


in reply to Automatic creation of a hash of hashes of arrays (?)

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1072262]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-19 14:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found