Here is a performance-improved version of perlcapt's solution - this one includes a response to your last query.
#!/usr/bin/perl -w use strict; use warnings; my %hash = ( 'a' => { 'x' => 40, 'z' => 102 }, 'b' => { 'z' => 100, 'x' => 10, 'y' => 20 }, 'c' => { 'x' => 50 }, 'd' => { 'z' => 101, 'y' => 30 } ); sub stats { my ($hhp, $SearchKey, $SearchVal) = @_; my %counters; # a counter hash of which keys are used at level two my $SearchCount; foreach my $key1 (keys %$hhp) { my @cList = keys %{$hhp->{$key1}}; ++$counters{join('+',sort @cList)}; ++$SearchCount if exists $hhp->{$key1}->{$SearchKey} and $hhp->{$key1}->{$SearchKey} == $SearchVal +; } return $SearchCount, %counters; } #### main my ($SearchCount, %result) = stats(\%hash,'z',101); foreach my $key (sort keys %result){ print "$key \t$result{$key}\n"; } print "Count of z==101 is $SearchCount\n"; __END__ ## OUTPUT ## x 1 x+y+z 1 x+z 1 y+z 1 Count of z==101 is 1

    Earth first! (We'll rob the other planets later)


In reply to Re: Counting hash of hashes elements by NetWallah
in thread Counting hash of hashes elements by albert

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.