andylevel2 has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Problem with while loop inside a foreach loop
by LanX (Saint) on May 03, 2013 at 11:53 UTC
    I'm pretty sure that using named variables instead of $_ in THREE nested loops solves the issue. :)

    Maybe you should take some looks into Damian's PBP?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Thanks all, that does solve the issue. I thought that the population of $_ (following the evaluation of some expresion) was a one way thing. The idea that changing $_ can work its way back to another variable will take a bit more thought on my part. Rolf, I like your diplomacy in suggesting "Perl Best Practices" :) I have just ordered it from the big south American river!
        > I have just ordered it from the big south American river!

        Great ... TheDamian owes me a drink now! ;-)

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: Problem with while loop inside a foreach loop
by hdb (Monsignor) on May 03, 2013 at 11:34 UTC
      Exactly. Looping over the file content the value of $_ is modified, which modifies the elements of @floors. The last value is undef (read at each file's end), so @floors ends up containing undefs after going through building1. Try adding the following line after the beginning of the foreach (@floors) loop:
      warn "FLOORS: @floors.\n";
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Problem with while loop inside a foreach loop
by InfiniteSilence (Curate) on May 03, 2013 at 14:44 UTC

    There were answers provided but I was missing the all critical, 'why does it happen this way?' One quick test shows that it probably should not:

    perl -e '$_ = 100; for(1..5){};print;'
    Produces '100', not '5'. Obviously for localizes $_, but apparently while does not...

    From Programming Perl Section 4.4.1: Unlike the foreach loop we'll see in a moment, a while loop never implicitly localizes any variables in its test condition. This can have "interesting" consequences when while loops use globals for loop variables.

    So you don't really need to add new variables at all. You need to force Perl to localize $_, like so:

    #... open FLOORFILE, $floorfile or die $!; { local($_); while (<FLOORFILE>) { print "$_\n"; } } #...
    Now the output is this:
    ... building2 floor1 aldfja;jd;af floor2 aldfja;jd;af floor3 aldfja;jd;af

    Celebrate Intellectual Diversity

      Produces '100', not '5'. Obviously for localizes $_, but apparently while does not...

      I do not know for sure how the compiler does this with $_, but I have noticed that for or foreach is safer than while, and that map or grep are much much safer than for or foreach.

      I was able to do deeply nested map instructions and get it right (almost) immediately, while the equivalent constructs with foreach did not work because of $_ corruption.

      As an example, consider this code, which I gave as an example of functional programming under Perl on another forum:

      while (my $line = <$FILE_IN>) { my ($start, $end) = split /##/, $line; my $out_line = $start . "##" . join ";", map {$_->[0]} sort {$a->[1] <=> $b->[1]} map { my ($id, @values) = split /;/, $_; @values = map { (split /-/, $_)[0]} @values; map {[$id, $_]} @values;} split /@/, $end; }

      I know that this is quite far-fetched LISP-like Perl code (I proposed this code just for the fun, not for serious consideration), but it does work perfectly on the data provided. Trying to replace some of the map instructions with some foreach instructions simply breaks every thing, presumably because the program gets confused about $_.

        map or grep are much much safer than for or foreach.

        No they aren't :) They alias just like for/foreach.

        I know that this is quite far-fetched LISP-like Perl code

        Not really :)

      Thanks for that, it does make it clearer.
      Thanks, that makes it clearer.
Re: Problem with while loop inside a foreach loop
by Anonymous Monk on May 03, 2013 at 17:48 UTC
    Simple... don't use $_.

    Instead: foreach my $current_building (@buildings) { ...

    Within the body of the loop, $current_building will be defined and will contain the current index of the loop.

      Anonymous Monk wrote:

      Within the body of the loop, $current_building will be defined and will contain the current index of the loop.

      Actually it will contain the array element not the index of the loop or index of the array. I assume that is what you meant.

        Actually it will contain the array element ...

        Actually, it will contain an alias to the array element. See choroba's reply.

        >perl -wMstrict -le "my @ra = (1, 2, 3, 4); print qq{@ra}; ;; for my $n (@ra) { $n *= $n; } print qq{@ra}; " 1 2 3 4 1 4 9 16