title: Perl; how to filter an hash by value (specifying a condition)
body: <p>I'm not very expert in perl language but I encountered a problem that I couldn't fix, even after a long research on the web.
  Briefly, I have an hash of hashes like this:</p>
  
  <pre><code>my %HoH = (
      chr1 =&gt; { start =&gt; 30, end =&gt; 55, },
      chr1 =&gt; { start =&gt; 18, end =&gt; 21, },
      chr1 =&gt; { start =&gt; 30, end =&gt; 80, }
  );
  </code></pre>
  
  <p>I simply would like to find a way to filter it ( I mean, obtaining a new hash of hashes in output) for particular values. In particular, given an interval, let's say 40-60, I want a new hash of hashes with only elements overlapping this interval.</p>
  
  <p>in other words I would like to get as output:</p>
  
  <pre><code>my %HoH = (
      chr1 =&gt; { start =&gt; 30, end =&gt; 55, },
      chr1 =&gt; { start =&gt; 30, end =&gt; 80, }
  );
  </code></pre>
  
  <p>As first attempt, I thought to try something like this:</p>
  
  <p>identify and then delete all elements with <code>"end" &lt; 40</code> and:
  identify and then delete all elements with <code>"start" &gt; 60</code>.</p>
  
  <p>So I just tried:</p>
  
  <pre><code>grep { $HoH{$_}{"end"} &lt; 40 } keys(%HoH); 
  delete $HoH{$_} for grep { $HoH{$_}{"end"} &lt; 40} keys(%HoH);
  </code></pre>
  
  <p>But just after the first of the two filters I found in the output only last element and I really  don't understand where is the mistake:</p>
  
  <pre><code>hash size is 1
  chr1: start=30 end=80 
  </code></pre>
  
  <p>printed out with the following:</p>
  
  <pre><code>my $len = keys %HoH;
  print "hash size is $len\n";
  
  foreach my $chr ( keys %HoH ) {
     print "$chr: ";
     for my $position ( keys %{ $HoH{$chr} } ) {
        print "$position=$HoH{$chr}{$position} ";
     }
     print "\n";
  }
  </code></pre>
  
  <p>It seems quite complex for me this time, I would be glad if somebody of you could give me some help.</p>