title: Searching within Hash of Hashes
body: <p>Say I have an array and a multidimensional hash. I want to recursively look whether the values in the array exist as keys in the hash. Is there any better way than doing it as follows? (example edited from Perl Maven<a href="http://perlmaven.com/multi-dimensional-hashes" rel="nofollow">Perl Maven</a>).</p>
  
  <p>Note a couple of things more:</p>
  
  <ol>
  <li>In the example below, only <code>FooBar</code> and <code>Mathematics</code> exist in the hash <code>%grades</code>;</li>
  <li>The main question in this post is how to find a more efficient way to search at first level, then second level, etc. etc., given that my real example has about 7 levels;</li>
  <li>Note that, my idea is to try to tighten the search the most possible. The best would be if all elements in my array could be found, by order, in the hash (i.e. having <code>@array=("FooBar","Mathematics")</code> to look into the values at <code>$myhash{"FooBar"}{"Mathematics"}</code>; if this fails, look whether "FooBar" or "Mathematics" exist at the second level of the hash, i.e. <code>$myhash{"otherkeys"}{"Mathematics"}</code> or <code>$myhash{"otherkeys"}{"FooBar"}</code>).</li>
  </ol>
  
  <p>Example:</p>
  
  <pre><code>#!/usr/bin/perl
  use strict;
  use warnings;
  use Data::Dump;
  my @subjectsandnames=("FooBar","ThatBar","Mathematics","Biology","Physics");
  my %grades;
  
  $grades{"FooBar"}{Mathematics}   = 12;
  $grades{"FooBar"}{Literature}    = 99;
  $grades{"PetiBar"}{Literature}   = 87;
  $grades{"PetiBar"}{Mathematics}  = 13;
  $grades{"PetiBar"}{Art}          = 93;
  foreach my $name (sort keys %grades) {
      my @possible = grep defined, map { $grades{$_}} @subjectsandnames; #sees matches at first level
      dd @possible;    
      foreach my $subject (sort keys %{$grades{$name}}){
          my @possible2 = grep defined, map { $grades{$name}{$_}} @subjectsandnames; #sees matches at second level
          dd @possible2;
      }
  }
  </code></pre>