Hi, I have been playing around with loops in perl and I have come across something that I don't understand. I have put together the following code to illustrate this (it is not part of a real program).
# floor1.txt # This files contains info about floor 1. # floor2.txt # This files contains info about floor 2. # floor3.txt # This files contains info about floor 3. use strict; use warnings; my @buildings = qw(building1 building2); my @floors = qw(floor1 floor2 floor3); my $current_building; my $floorfile; foreach (@buildings) { $current_building = $_; print "$current_building\n\n"; foreach (@floors) { $floorfile = $_ . ".txt"; print "$_\n"; # Why does the while loop, below, mess up the 2nd iteration of the for +each? # open FLOORFILE, $floorfile or die $!; # while (<FLOORFILE>) # { # print "$_\n"; # } } print "\n\n"; }
As shown in the comments I have 3 text files (floor1.txt etc) which just contain a single line of text. When the while loop is commented out (as above) both foreach loops work fine and print out 3 floors on building 1 and 2, but when I try to use the content of the floor text files the code fails when it gets to building 2. What obvious thing am I missing here? Regards, Andy.

In reply to Problem with while loop inside a foreach loop by andylevel2

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.