Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re: Number of values for each key in hash by Anonymous Monk
in thread Number of values for each key in hash by Sofie

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2021-10-27 14:16 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (93 votes). Check out past polls.