title: Hash of hashes: How to get the number of occurrences of a key?
body: <p>I have the following text file.</p>
  
  <pre><code>foo1    bam
  foo1    bam
  foo2    bam
  foo1    zip
  foo2    boo
  foo1    zip
  foo3    zip
  </code></pre>
  
  <p>I would like to make a Hash of Hashes where KEY1 is <strong>column one</strong>,  KEY2 is the sound it makes (<strong>column two</strong>): <code>bam</code>, <code>zip</code>, or <code>boo</code>, and the VALUE is the <strong>number of occurrences</strong> of that sound. Such that the data structure is like this:</p>
  
  <pre><code>$VAR1 = {
        'foo1' =&gt; {
                           'bam' =&gt; [
                                      2
                                    ],
                           'zip' =&gt; [
                                    2
                                  ],
                         },
        'foo2' =&gt; {
                          'bam' =&gt; [
                                   1
                                 ],
                          'boo' =&gt; [
                                   1
                                 ],
                        },
          'foo3' =&gt; {
                          'zip' =&gt; [
                                    1
                                  ],
                      }
           }
  </code></pre>
  
  <p>Here's what I have so far</p>
  
  <pre><code>use strict; use warnings;    
  open(my $fh, '&lt;', 'file.txt') or die $!;
  my %HoH;
  while(&lt;$fh&gt;){
      chomp;
      my @cols = split(/\t/, $_);
      my $KEY1 = $cols[0];
      my $KEY2 = $cols[1];
      push( @{$HoH{$KEY1}{$KEY2}}, 1); # This actually creates a hash of hash of arrays
  }
  
  my %HoH_final;
  foreach my $KEY1 (%HoH) {
      foreach my $KEY2 (keys %HoH{$KEY1}){
      my $count = scalar @{$HoH{$KEY1}{$KEY2}}; # get the size of that array
          push( @{$HoH_final{$KEY1}{$KEY2}}, $count);
     }
  }
  </code></pre>
  
  <p>What do you think?</p>