Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Answer: How do I modify the KEYS in a hash (copy)

( #376392=categorized answer: print w/ replies, xml ) Need Help??

Q&A > hashes > How do I modify the KEYS in a hash (copy) contributed by NetWallah

To modify keys of a hash, it is necessary to copy it. The keys can be modified in the process of the copy by using 'map' like this:
my %Orig=('a b'=>44, 'c d'=>22, rr=>99); my %Copy = map {my $y=$Orig{$_}; #Save Copy of Val tr/ /_/; $_ => $y} keys %Orig; print qq(C: $_ => $Copy{$_}\n) for keys %Copy; print qq(O: $_ => $Orig{$_}\n) for keys %Orig;
The trick here is to save a copy of the Value into "$y" before "$_" is modified.
Once $_ is modified, the $Orig{$_} is no longer valid, so it must be saved into $y first.

Comment on Answer: How do I modify the KEYS in a hash (copy)
Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-08-20 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (111 votes), past polls