title: Adding each element in an array to a complex hash in Perl
body: <p>I have an array with n number of elments. I want to add each of the elements to a complex hash, each as a key/value pair. If the number of elements were fixed, say three, I would do:</p>
  
  <pre><code>my %hash;
  my @array = ("first", "second", "third");
  $hash{$array[0]}{$array[1]}{$array[2]}++;
  </code></pre>
  
  <p>The structure I want to end up with, is this (printed with Data::Dumper): </p>
  
  <pre><code>$VAR1 = 'first';
  $VAR2 = {
          'second' =&gt; {
                     'third' =&gt; 1
  };
  </code></pre>
  
  <p>But I am at loss at achieving the same structure when the number of elements in the array isn't fixed. Something with anonymous variables and iterating through the array, yes, but something like <pre><code><code>foreach @array{$hash{$_}++};</code></pre></code> will obviously only make one entry per element, and not the desired structure. Help?</p>