I have what I think is a scoping problem with a set of two foreach statements.  I have two tables in mySQL:  one with employee info., in particular employee id's and employee names;  the other table has information on each employee's work schedule for each day.  I'm trying to loop through the list of employees and then for each employee, loop through the schedule records for each day relavent to each employee.  Note, there are only five records for each employee since we only schedule one work week at a time.

Below is an excerpt of my code.  I tried to cut it down to only the relevant pieces.  So, please fill in the missing pieces with your imagination.

# After the initial, usual stuff I query mySQL using # DBI and extract employee id numbers and their names # for an array and a hash. while (@emp_list = $sth->fetchrow_array()) { push(@emps, $emp_list[0]); $employees{$emp_list[0]} ="$emp_list[1]"; } $sth->finish(); my @days = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri'); foreach $emp_id(@emps) { print "Employee: ", $employees{$emp_id}, "\n"; print "Employee ID: ", $emp_id, "\n"; foreach $wkday(@days) { $sql_str = "SELECT emp_name, field2, field3 FROM table1 WHERE emp_id='$emp_id' AND wkday='$wkday'"; $sth= $dbh->prepare($sql_str); $sth->execute(); @schedule = $sth->fetchrow_array(); } print @schedule; # Actually I break the @schedule up nicer than this. }

The problem seems to be that the value of the variable $emp_id does not penetrate the second or inner foreach loop.  The result is that $emp_id for the loop regarding the schedule is blank, although the $emp_id does print on the header line above it.  How do I pass the value of a variable (e.g., $emp_id) of one foreach loop onto another foreach loop?

That's Spenser, with an "s" like the detective.


In reply to foreach within a foreach by Spenser

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.