Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by NetWallah (Abbot)
on Jul 21, 2004 at 21:45 UTC ( #376392=categorized answer: print w/replies, xml ) Need Help??

Q&A > hashes > How do I modify the KEYS in a hash (copy) - Answer 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2017-05-30 09:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?