Always include use strict;
and use warnings;
at the top of EVERY perl script.
use strict;
use warnings;
my %Region = (1 => {name => "nameVal"}, 2 => {name => "nameVal2"});
while(my($k, %v) = each %Region) {
print $k, $v{'name'}, "\n";
}
In this case, the warning would have been the following:
Reference found where even-sized list expected at scratch.pl line 5.
This is pointing out the fact that you're assigning single value, a reference, to a hash %v
which expects an even-sized list, or key value pairs.
So we fix that line so it's assigning a reference to a scalar:
while(my($k, $v) = each %Region) {
You will now get the following error:
Global symbol "%v" requires explicit package name at input7.pl line 6.
There is no definition for %v
, only our scalar with a reference, $v
. To dereference that variable and access a value, we use the arrow operator ->
print $k, $v->{'name'}, "\n";
Output is now what you desire:
2nameVal2
1nameVal
The lesson here is ALWAYS include use strict;
and use warnings;
at the top of every perl script. For more reasons why, check out: Why use strict and warnings?