in reply to Arrays and Handles Problem

Well, you're using $_ as the loop variable in two nested loops. That's bound to cause trouble. Use an explicit loop variable in at least one of the loops and the problem should go away.

Also, why the nested loop in the first place? Normally it makes no sense to read everything from DATA more than once.


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re^2: Arrays and Handles Problem
by Melly (Chaplain) on May 02, 2019 at 12:28 UTC

    Yes, __DATA__ is a bit of a red herring, since originally it was multiple reads of an external text file.

    That aside, thanks all for the explanation (and the fix). I must admit I thought I'd tried the solution already, but obviously not... also, I thought it was okay to use multiple nested instances of $_, and that each was local to its block, but clearly not...

    map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
    Tom Melly, pm (at) cursingmaggot (stop) co (stop) uk
      I thought it was okay to use multiple nested instances of $_, and that each was local to its block, but clearly not...

      That's true for foreach, but not while (<>):

      use warnings; use strict; use Data::Dump; $_ = "foo"; for ("x","y") { dd $_; } dd $_; while (<DATA>) { dd $_; } dd $_; __DATA__ hello

      Output:

      "x" "y" "foo" "hello\n" undef