I’ll attempt to illustrate this with an example. Take a common example of a Hash of Hashes:
my %HoH = (
flintstones => {
lead => "fred",
pal => "barney",
},
jetsons => {
lead => "george",
wife => "jane",
"his boy" => "elroy",
},
simpsons => {
lead => "homer",
wife => "marge",
kid => "bart",
},
);
For my purposes, I would like to be able to add an unnamed, or anonymous hashes to %HOH. I won’t need (or be able to) define these sub-hashes until runtime. How can I accomplish this with Perl?
Everything I’ve read (and I have read through Perldocs and Google’d already) seems to show examples where all sub-hahes (e.g. “flintstones”, “jetsons”, and “simpsons”) are defined.
What I am doing is attempting to build a parent Hash that will contain sub-hashes with rows from a CSV file:
%TopHash = (
%Line1 => {
cell01 => $some_value1a;
cell02 => $some_value2a;
cell03 => $some_value3a;
},
%Line2 => {
cell01 => $some_value1b;
cell02 => $some_value2b;
cell03 => $some_value3b;
},
%Line3 => {
cell01 => $some_value1c;
cell02 => $some_value2c;
cell03 => $some_value3c;
},
# etc
# etc
# etc
);
The number of “%LineX” hashes that I need is not known until runtime (because they represent the number of lines in a CSV that is read at runtime).
Any ideas? If it isn’t clear already…I still am trying to wrap my head around Perl hashes.
First you create the hash from the current line you’re parsing
or create a reference to a hash outright
Then you add it to your overall hash, remembering that nested perl structures just contain references to the other structures.
Updated
Example given a loop over an array of data to parse