laziness, impatience, and hubris | |
PerlMonks |
Re: can I change hash keys or values directlyby Marshall (Canon) |
on Jan 30, 2021 at 03:20 UTC ( [id://11127676]=note: print w/replies, xml ) | Need Help?? |
What you want to do is actually fairly straight-forward. For each existing key, see if there is a new name for that key. If there is, then delete the current hash entry and create a new entry with the new name and with the current value.
Here is some simple example code.
Anyway, modify this code to meet your requirements and to take into account what I mentioned. Update: It may not be obvious to you, but changing the name of a single hash key (whether longer or even shorter) could potentially cause Perl's internal representation of the hash table to be completely recalculated and reorganized. In general as a Perl'er, don't worry about it. In the internal "guts", the Perl code that deals with hashes is written in C and I must add that it is quite efficient at what it does and how it does it. Update2: Every map statement IS a loop! It may not look that way to you, but that is what it is. Shorter source code does not necessarily equate with shorter or even more efficient executed code. Update3: Added scalar value of a %hash to the code.
In Section
Seekers of Perl Wisdom
|
|