Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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?
[choroba]: erix you there? I have a Pg question...
[erix]: yeah... trepidation
Corion quivers with antici...
[choroba]: if you turn off autocommit and run a prepared statement which fails, the transaction is automatically rolled back, at least in DBD::Pg
[erix]: at least, I'll know where to find the documentation :P
[choroba]: but if there's no prepared statement, there's no rollback

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2017-09-19 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (220 votes). Check out past polls.

    Notices?