in reply to Learning to use "each"

I don't think Foreach Loops support multiple iterator variables like that. Take a look at this B::Deparse output (tip #6 from Basic debugging checklist):
$ perl -MO=Deparse 901847.pl use warnings; use strict 'refs'; { my(%hash) = ('one', 1, 'two', 2); foreach $_ (my($key, $value) = each %hash) { print $key, "\n"; } }
It shows that the loop is really using $_ for the iterator.

Update: Now I have changed the OP code a little, just to print more info:

use warnings; use strict; my %hash = ("one" => 1, "two" => 2, ); foreach ((my $key, my $value) = each %hash) { print "\$key=$key \$value=$value \$_=$_\n"; } __END__ $key=one $value=1 $_=one $key=one $value=1 $_=1

It seems like each is evaluated once in list context. It happens to choose the 'one' key and stores that in $key and stores the value '1' in $value. It never evaluates each again. Then each time through the loop, each item of the list ('one', '1') is assigned to $_.

Replies are listed 'Best First'.
Re^2: Learning to use "each"
by JavaFan (Canon) on Apr 28, 2011 at 19:14 UTC
    Explaination:
    That prints 'one' twice. It's iterating over the return value of each, so over a single $key, $value pair, printing the value of $key twice.
      I don't think that's what the OP wants.
      I think you misunderstood my post. I simply ran the OP's foreach code through the deparser to show how perl interprets the code. I simply showed the output of the OP's deparsed code. I did not suggest any new code.
        Yes, I realized and updated my post before you finished replying.