Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying Key in hash table?

I was just wondering if you can modify the key in a hashtable. I have been able to successfully modify the value attached to the key but have been unable to modify the actually key.

$names = @{Tree = "1"; Forest = "2"}

Take for example the code above, I'd like to change the "Tree" to something other than tree.

like image 505
OysterMaker Avatar asked Feb 20 '26 07:02

OysterMaker


1 Answers

No, you cannot do this directly. Instead, you will need to make a new key that has the same value as "Tree" and then delete the "Tree" key when you are done. Below is a demonstration:

PS > $names = @{Tree = "1"; Forest = "2"}
PS > $names.NewKey = $names.Tree
PS > $names.Remove("Tree")
PS > $names

Name                           Value                                                
----                           -----                                                
NewKey                            1                                                    
Forest                            2                                                    


PS > 

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!