I want to push a reference to a hash. By that I mean I want to push a reference to a new hash that is a shallow copy of the hash I am given.
How do I create the shallow copy?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Just copy it:
If you want another reference, just expand the original reference in the anonymous hash constructor:
For those wondering about shallow copies: this sort of assignment only makes new values for the top level keys. Any values that are references will still be the same reference in the new hash. That is, you can replace a value in the new hash without changing the original hash. If you merely change the value, such as pushing a new item onto an anonymous array value, both hashes get the change because they share the same reference. As such, shallow copies are usually not what you want.