Experts - Any advice on how to get a hash value in a foreach loop?
Values print correctly on standalone print statements, but I can't access value
in foreach loop.
See sample code below and thanks in advance.
foreach my $z (sort keys %hash) {
for $y (@{$hash{$z}}) {
print "$z ->"; # Prints key1
print "$y ->"; # Prints Value1
print $hash1{"$y"},"\n"; # Prints NULL <-- Why is this NULL??
}
}
print $hash1{"Value1"}; # Prints HashValue <-- I want this value in foreach
Notice that 'space Value1' is not the same that 'Value1' . Is that a copy and paste from your output?
If that is the case, $hash1{$y} or $hash1{"$y"} is undefined, since it would be equivalent to $hash1{' Value1'} and the key dereference should be $hash1{'Value1'}
The '-' is not a problem since the key is a string; what is a problem is that ' Value-1' is not the same that 'Value-1' , since the the space in front counts as part of the key.
There is no $hash1{' Value-1'} , but there is a $hash1{'Value-1'}
Correct what it would become the keys, in the array, in %hash
Thanks Aia!
I was stumped, big time. I appreciate your time looking into this for me.
I also wanted to tell you that I learn a lot from your posts on this forum.