use strict;
use warnings;
my %table = qw/schmoe joe smith john simpson bart/;
my $da =1;
my($key, $value); # Declare two variables at once
while ( ($key, $value) = each(%table) )
{
$table{$key} = ++$da;
}
print %table;
the output:
schmoe2smith3simpson4
I was expecting to get to 2345...
What is wrong with my foreach?
I think you misunderstood something.
The values are changed, but when you code
print %table;, you print keys and values.With
Data::Dumpermodule, it’s more clear :If you want to iterate through the hash, you can do :
but the hash name is confusing, it’s not a table but a hash.
Another solution to iterate through the hash :