Long time listener, first time caller. Love the show.
I gave up on my employer ever having a formal org chart, so I've started piecing one together with information available from our time and attendance package.
I've got the following information for each employee:
Department ID, employee ID, boss ID, employee name, department description.
I've built a hash of %boss, which contains:
Okay, you're right. This isn't the Real Information I'm working with, it's just sample data! The problem still shows with this small data set, however.$VAR1 = { '4' => '3', '1' => '1', '3' => '2', '2' => '1' };
In other words, employee ID 1 is the big kahuna in charge. Employee 2 works for 1, 3 works for 2, and 4 is the low man on the totem pole that actually does the work. *cough*
Here's my subroutine, which is passed an employee ID:
Here's the output I'm getting:sub getreports { my $target = shift(@_); while (my ($employeeid, $bossid) = each %boss) { print "scanning data for x, $target: $employeeid, $bossid\n" +; if ($bossid eq $target) { print "\n$bossid $name{$bossid} $area{$bossid}\n-> $em +ployeeid $name{$employeeid}\n"; if ($hasreports{$employeeid}) { print "$name{$employeeid} has subordinates\n"; print "getting employees of $employeeid\n"; getreports($employeeid); } } } print "exiting while % boss with target of $target\n\n"; }
This seems to be willing to loop forever, starting over at the beginning of the hash. What have I done wrong?scanning data for x, 2: 4, 3 scanning data for x, 2: 1, 1 scanning data for x, 2: 3, 2 2 big boss corner office -> 3 small boss small boss has subordinates getting employees of 3 scanning data for x, 3: 2, 1 exiting while % boss with target of 3 scanning data for x, 2: 4, 3 scanning data for x, 2: 1, 1 scanning data for x, 2: 3, 2
Humbly,
Anonymous Monk
In reply to Iterating over a hash, recursively, forever! by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |