Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Hash substitution

by suaveant (Parson)
on Oct 09, 2008 at 12:41 UTC ( [id://716201]=note: print w/replies, xml ) Need Help??


in reply to Hash substitution

You can't change the key of a hash because the key decides how to find the data based on an algorithm.

You must instead move the value, easiest done this way:

$hash{newkey} = delete $hash{oldkey};
The delete removes the value from the old hash location and returns the value, allowing you to put it in the new hash location.

                - Ant
                - Some of my best work - (1 2 3)

Replies are listed 'Best First'.
Re^2: Hash substitution
by Narveson (Chaplain) on Oct 09, 2008 at 12:49 UTC
    use Data::Dumper; my %hash = ( apple => 'red', orange => 'orange', banana => 'yellow', ); my $old_key = 'orange'; my $new_key = 'carrot'; $hash{$new_key} = delete $hash{$old_key}; print Dumper(\%hash);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://716201]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 03:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found