Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Hash Ref: Need to find out how many children I have :-)

by tirwhan (Abbot)
on Dec 16, 2005 at 10:02 UTC ( [id://517192]=note: print w/replies, xml ) Need Help??


in reply to Hash Ref: Need to find out how many children I have :-)

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://517192]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-26 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found