|We don't bite newbies here... much|
Re^5: 3D array of hashesby Rhys (Pilgrim)
|on Oct 08, 2003 at 22:12 UTC||Need Help??|
Let's separate the two problems. The first problem is the structure you're going to use to STORE the data. The second problem is how to READ an existing data set into this new storage mechanism.
The first problem is the easier one. You can get what you're looking for using nested hashes (e.g. "hashes of hashes"). At each level, the values in the hash are references (similar to pointers) to the hashes at the next layer "in". At the deepest layer, the values are simply values that have some arbitrary meaning.
So in the case of %weather_archive, to store the data you show, I would do:
...and so on. No data is stored for any year, month, or day that hasn't explicitly been stored.
NOTE: That's not to say that resources haven't been expended. Perl will allocate a certain amount of memory to all of these layered hashes - and four layers is pretty deep - but it shouldn't be too hideous. I'm using four-deep hashes in one of my Perl scripts without any trouble.
Anyway, to print out the data, you could do something like this (although hopefully not the same, since I think you could do the print part better than this):
One thing that might be difficult is making sure the months appear in the correct order. You might get around that by naming them "01-January" through "12-December" so you could use "sort keys" in the second loop.
As for reading in the data, that's just a matter of making sure that wherever you get your info, $year, $month, $day, $var, and $value are correct before you do:
However you loop through your input data depends solely on it's format, but the above statement is all you need to do to populate the hashes.