Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-19 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found