Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print nested hash with variable depth

Status
Not open for further replies.

RottPaws

Programmer
Mar 1, 2002
478
US
Does anyone have or know of a tutorial on printing/looping through a nested hash with a variable number of nests? I know how to do it when the depth of the nesting is fixed and known, but when it's variable, I don't know of an efficient way to code it.

And I haven't figured out the correct keywords to find an example.

For a universal example of what I'm trying to do, lets look at an organization chart:
[tt]Director-1
Admin-1
Manager1-1
Supervisor-111
Worker-1111
Worker-1112
...
Supervisor-112
Worker-1121
...
Manager-12
Project Manager-121
Supervisor-121
Worker-1211
...
...
...
Director2
...[/tt]

If I was writing a report that would always pull the supervisors under a manager and the workers under them, I could use 2 nested foreach loops to output the data.

But what about when the manager has a project manager who doesn't have subordinates? Or what if I want to use that same report to pull everybody under a director or a VP and the nesting is 4 or 5 levels deep instead of 2? And they have administrative assistants who also don't have subordinates . . .





_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
You'll need to recursively loop through all the nested hashes. I'm not sure what your data structure looks like exactly, but this should give you a start.
Code:
my %org_chart;
$org_chart{"Director-1"}{"Admin-1"} = 1;
$org_chart{"Director-1"}{"Manager-1"}{"Supervisor-111"}{"Worker-1111"} = 1;
$org_chart{"Director-1"}{"Manager-1"}{"Supervisor-111"}{"Worker-1112"} = 1;
$org_chart{"Director-1"}{"Manager-1"}{"Supervisor-112"}{"Worker-1121"} = 1;
$org_chart{"Director-1"}{"Manager-1"}{"Supervisor-112"}{"Worker-1122"} = 1;
$org_chart{"Director-1"}{"Manager-12"}{"Project Manager-121"} = 1;
$org_chart{"Director-1"}{"Manager-12"}{"Supervisor-121"}{"Worker-1211"}{"Apprentice-1201"} = 1;

print_nested_hash(\%org_chart, 0);

sub print_nested_hash {
    my $hash_ref = shift;
    my $depth = shift;
    foreach my $key (sort keys %{$hash_ref}) {
        print '    ' x $depth, $key, "\n";
        
        if (ref($hash_ref->{$key}) eq "HASH") {
            print_nested_hash($hash_ref->{$key}, $depth+1);
        }
    }
}
Which gives the output:
Code:
Director-1
    Admin-1
    Manager-1
        Supervisor-111
            Worker-1111
            Worker-1112
        Supervisor-112
            Worker-1121
            Worker-1122
    Manager-12
        Project Manager-121
        Supervisor-121
            Worker-1211
                Apprentice-1201
 
Thanks!

I'll give it a shot.

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
A tiny modification to make it more intuitive to call. Replace the relevant lines with:
Code:
print_nested_hash(\%org_chart);

sub print_nested_hash {
    my $hash_ref = shift;
    my $depth = shift || 0;
 
I think Data::Dumper provides a similar functionality, and you can override the formatting defaults.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top