Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Number of values for each key in hash

by Anonymous Monk
on Mar 02, 2020 at 01:20 UTC ( #11113610=note: print w/replies, xml ) Need Help??


in reply to Number of values for each key in hash

It occurs to me that when you say, "I would like to create a hash where the gene is the key and the genotype is the value," you are actually describing a so-called "XY Problem." Because you then go on to say that what you really want is: "how many genes have a specific genotype."

I'm not going to write code for you now, but this indeed is the essence of Perl's "auto-vivification" feature. Because this will essentially allow you to write what you're looking for as $hash{$gene}{$genotype}++.

The data-structure that you actually need to solve your problem is a "hash of hashes," with the final target being an integer counter.   At the end of the day, you want to have a first-level hash of genes, each bucket of which contains a hash of genomes, each bucket of which contains a count.   Auto-vification wll take you there. If a first-level hash key (gene) does not exist, Perl will silently create it. Then, if the second level key (gene, genome) does not exist, Perl will repeat the favor.

Yes, the Perl language was not created by language-purists. It was created by people who had a job to do.

  • Comment on Re: Number of values for each key in hash

Replies are listed 'Best First'.
Re^2: Number of values for each key in hash
by Your Mother (Bishop) on Mar 02, 2020 at 01:32 UTC
    I'm not going to write code for you

    What a fascinating idea. Fortunately for the OP other monks answered this yesterday and included not just code but analysis and helpful elaboration.

    Im going to imagine Kermit the frogs apoplectic handwaving from now on. Its just a job I have to do.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2020-10-22 21:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (230 votes). Check out past polls.

    Notices?