On 11/10/09 Tue  Nov 10, 2009  1:22 PM, "Bryan R Harris"
<bryan_r_har...@raytheon.com> scribbled:

> 
> 
> I have a curiosity maybe someone here can help with.
> 
> This code:
> 
>    @a=(1,2);
>    map { $_ = 3 } @a;
>    print join(",", @a), "\n";
> 
> ... prints "3,3".  That map is changing the @a array as it goes through it.
> Good.
> 
> Now this:
> 
>    %a=(1,2);
>    map { $_ = 3 } keys %a;
>    print join(",", keys(%a)), "\n";
> 
> I expected this to print "3", but it doesn't -- it prints "1".  If map sets
> $_ as an alias to the value, why isn't it changing the keys?

Because the keys() subroutine generates a list of keys that are not aliases
to the keys in the hash.

It is not a good idea to change the "values" of keys in a hash, because
finding the (key,value) pair for a specific key depends upon a key value
being located in a specific location ("bucket") within the hash. If you
change the key value in place, the hash access algorithm will no longer be
able to find the (key,value) pair.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to