The problem is with the nature of the
each function. This will return keys of the hash in order, until it returns false when there are none left. Then it starts over again.
The problem now is that when you call you function recursively, the call to each isn't getting reset. So you go through the first three key/value pairs in the top function, then your subordinates search that you performed recursively is given the last key/value pair when it first evaluates each. Then, it gets
undef, which is why it exits prematurely. Now that the hash has returned undef, it starts from the beginnig with the original function again. The global nature of %boss ensures that your recursion doesn't work.
To stop this behavior, begin and end your function with
keys %boss This will ensure that you start anew with the values. If it is possible to have a structure where a person has more than one subordinate, like this:
$VAR1 = {
'4' => '3',
'1' => '1',
'3' => '2',
'2' => '1',
'3' => '5'
};
Then you will want to try assigning a temp hash before you call recursively. Though this isn't tested, you could try doing this at the top of your function:
my %temp_boss = %boss;
while (my ($employeeid, $bossid) = each %temp_boss) {
I think that recursive functions play nicely with lexical scoping, but don't quote me on that.
HTH.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.