in reply to Re^3: Hash assignment: Undocumented feature?
in thread Hash assignment: Undocumented feature?
No, no. I am not surprised at all (nor new to the fact) that $hash{'bucket'} = 'something'; overwrites anything that took 'something''s place before. (I thought I had hinted as much in my original post; sorry if that wasn't clear enough.) That was in fact the one and only way of modifying a hash value I knew.
For that reason, it was clear to me that if you specify a key twice, one of them must have to go, since otherwise, the hash is no longer a hash. But it wasn't obvious to me which of the values would prevail. Even though the last one is arguably the most intuitive choice, I wouldn't have wanted to bank on it.
What contributed to my wondering was probably this easy interchangeability of arrays and hashes: ('foo', 2, 'bar', 'pepper') can be assigned to @some_array_variable or %some_hash_variable, or you could assign it to the former and then say %some_hash_variable = @some_array_variable. But
%some_hash_variable = ('foo', 2, 'bar', 'pepper', 'foo', 4); # is crucially different from @some_array_variable = ('foo', 2, 'bar', 'pepper', 'foo', 4);
That's what I found interesting about it, and no, I did not find it completely obvious which value %some_hash_variable would have. (My secret motto is "You never know with Perl".) But now I know. It's a nice feature, and now I can rely on it.
|
|---|