Re: Problem with while loop inside a foreach loop
by LanX (Saint) on May 03, 2013 at 11:53 UTC
|
| [reply] |
|
|
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!
| [reply] |
|
|
| [reply] |
Re: Problem with while loop inside a foreach loop
by hdb (Monsignor) on May 03, 2013 at 11:34 UTC
|
| [reply] |
|
|
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";
| [reply] [d/l] [select] |
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
| [reply] [d/l] [select] |
|
|
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 $_.
| [reply] [d/l] |
|
|
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 :)
| [reply] |
|
|
|
|
Thanks for that, it does make it clearer.
| [reply] |
|
|
Thanks, that makes it clearer.
| [reply] |
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. | [reply] |
|
|
| [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
| [reply] [d/l] |