How come 'Value2' is changed to 'Value_two', but 'key2' is unchanged?Because the keys aren't dealt out into $_ in the same way as the values. Consider:
qwurx [shmem] 15:35 ~ > perl -MDevel::Peek -e '%hash = qw(a 1 b 2 c 3) +; for(%hash){print STDERR "$_: ";Dump $_; s/b/d/;} print "$_ => $hash +{$_}\n" for sort keys %hash' c: SV = PVIV(0x8168b10) at 0x8166b44 REFCNT = 2 FLAGS = (POK,FAKE,READONLY,pPOK) IV = -289776295 PV = 0x8187928 "c" CUR = 1 LEN = 0 3: SV = PV(0x8167ae8) at 0x8167894 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x818f380 "3"\0 CUR = 1 LEN = 4 a: SV = PVIV(0x8168b20) at 0x8166cf4 REFCNT = 2 FLAGS = (POK,FAKE,READONLY,pPOK) IV = -902917054 PV = 0x817be80 "a" CUR = 1 LEN = 0 1: SV = PV(0x8167b00) at 0x8166c28 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x817ecb0 "1"\0 CUR = 1 LEN = 4 b: SV = PVIV(0x8168b50) at 0x818dfd8 REFCNT = 2 FLAGS = (POK,FAKE,READONLY,pPOK) IV = 14385563 PV = 0x81876b0 "b" CUR = 1 LEN = 0 2: SV = PV(0x8167ba8) at 0x8166d90 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x81866b0 "2"\0 CUR = 1 LEN = 4 a => 1 b => 2 c => 3
The flags for a hash key are (POK,FAKE,READONLY,pPOK), for values they are (POK,pPOK). Which means you manipulate the actual content modifying a value, but you're operating on fake variables modifying the keys.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
In reply to Re^4: Modifying the keys in a hash
by shmem
in thread Modifying the keys in a hash
by Mandrake
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |