I am attempting to initialize the values of keys of hashes from a single hash. Instead of all the hashes being initialized by value, the values are all references to the same hash, which I don't want. What is the best way to set up all the hashes with the same structure/values of the one hash without just passing reference?
In the example below, I would like the values of hash3 and hash4 to be the key/value pairs of hash1 and hash2. However, what I am getting is the values are being set to references, which means I cannot change the values of hash3 and hash4 independently.
use warnings;
my %hash1 = (
bush => "A",
tree => "A"
);
my %hash2 = (
dog => "A",
cat => "A"
);
my %hash3 = (
plants => \%hash1,
animals => \%hash2
);
my %hash4 = (
plants => \%hash1,
animals => \%hash2
);
print "$hash3{plants}{bush}\n";
print "$hash4{plants}{bush}\n";
print "\n";
$hash4{plants}{bush}="B";
print "$hash3{plants}{bush}\n";
print "$hash4{plants}{bush}\n";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.