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 $_.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.