Update: Oops, misread the question. Clearly not the answer.

Update2: This should do it though :-)

Something like this?

use warnings; use strict; my %count; my $hash = {Grand => { Parent1 => {Child1 => { name => "me", age => 99 }, Child2 => { name => "you", age => 42 }, Child3 => { name => "she", age => 1 }}}}; recurse_or_count($hash->{Grand},"Grand"); sub recurse_or_count { my ($cur,$string)=@_; for my $elem (keys %$cur) { if (ref($cur->{$elem}) eq "HASH") { $count{$string}++; recurse_or_count ($cur->{$elem},$string."->$elem"); } else { $count{$string}->{$elem}=$cur->{$elem}; } } } for my $key (sort keys %count) { if (ref($count{$key}) eq "HASH") { for my $data (sort keys %{$count{$key}}) { print "$key has $data $count{$key}->{$data}\n"; } }else { my $child = $count{$key} > 1 ? "children" : "child"; print "$key has $count{$key} $child\n"; } } __OUTPUT__ Grand has 1 child Grand->Parent1 has 3 children Grand->Parent1->Child1 has age 99 Grand->Parent1->Child1 has name me Grand->Parent1->Child2 has age 42 Grand->Parent1->Child2 has name you Grand->Parent1->Child3 has age 1 Grand->Parent1->Child3 has name she

Update3: and if you don't mind the child data being printed out before the child count, here's a more efficient version of that:

use warnings; use strict; my %count; my $hash = {Grand => { Parent1 => {Child1 => { name => "me", age => 99 }, Child2 => { name => "you", age => 42 }, Child3 => { name => "she", age => 1 }}}}; recurse_or_count($hash->{Grand},"Grand"); sub recurse_or_count { my ($cur,$string)=@_; for my $elem (sort keys %$cur) { if (ref($cur->{$elem}) eq "HASH") { $count{$string}++; recurse_or_count ($cur->{$elem},$string."->$elem"); } else { print "$string has $elem $cur->{$elem}\n"; } } } for my $key (sort keys %count) { my $child = $count{$key} > 1 ? "children" : "child"; print "$key has $count{$key} $child\n"; } __OUTPUT__ Grand->Parent1->Child1 has age 99 Grand->Parent1->Child1 has name me Grand->Parent1->Child2 has age 42 Grand->Parent1->Child2 has name you Grand->Parent1->Child3 has age 1 Grand->Parent1->Child3 has name she Grand has 1 child Grand->Parent1 has 3 children

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan

In reply to Re: Hash Ref: Need to find out how many children I have :-) by tirwhan
in thread Hash Ref: Need to find out how many children I have :-) by rupesh

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.