Since the 'tr' function returns the number of translations, and we do not need that number, it is essentially discarded, using the ternary "?" operator. The KEY we need is contained in "$_", with is returned under both TRUE and FALSE conditions of the "?" operator. "$_" contains the translated KEY we need.my %HashCopy = map {tr/ /_/? $_:$_ => $OrigHash{$_}} keys %OrigHash; # Above line copies %OrigHash and Replaces Spaces with Underscore +in hash Key
The VALUE is obtained simply by indexing %OrigHash.
If anyone has an insight into the inner workings of "map" - specificaly, how does it know whether to use the translated or untranslated version of "$_" when interpreting "=> $OrigHash{$_}", please post that info here. The bottom line is that it DOES work using the original $_, not the translated one, but WHY ? Is the right-side of the "=>" evaluated first ?
Originally posted as a Categorized Answer.
In reply to Re: How do I modify the KEYS in a hash (copy)
by NetWallah
in thread How do I modify the KEYS in a hash (copy)
by NetWallah
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |