Dear Monks,

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:

$VAR1 = { '4' => '3', '1' => '1', '3' => '2', '2' => '1' };
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.

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:

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"; }
Here's the output I'm getting:

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
This seems to be willing to loop forever, starting over at the beginning of the hash. What have I done wrong?

Humbly,
Anonymous Monk


In reply to Iterating over a hash, recursively, forever! by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.