in reply to Re: Strange loop output
in thread Strange loop output

It doesn't seem to matter where I print. I just took it out of the loop and the same problem occurs!

Replies are listed 'Best First'.
Re^3: Strange loop output
by packetstormer (Monk) on Apr 21, 2011 at 08:18 UTC
    Actually - it does matter where I print it! Moving it outside the loop makes it work correctly - thanks. I didn't know, and still don't, how that makes a difference!

      Consider a very simple loop like this:

      my @a; for (qw/a b c/) { push @a, $_; print (join ', ', @a), "\n"; }

      In the first iteration, @a contains a, so the output is

      a

      In the second iteration, @a contains a, b, so the total output is

      a # output from previous iteration a, b # output from current iteration

      In the third iteration it will print a, b, c, so you'll see a total of three a in the output - probably the duplicates you complained about.

      If you move the print out of the loop, you just get a, b, c because that's what @a contains.