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:

$weather_archive{'2003'}{'April'}{'15'}{'High'} = '73f'; $weather_archive{'2003'}{'April'}{'15'}{'Low'} = '49f';
...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):

foreach $year ( sort keys %weather_archive ) { print "$year\n"; foreach $month ( keys %{$weather_archive{$year}} ) { print " $month\n"; foreach $day ( sort keys %{$weather_archive{$year}{$month}} ) { print "$day\n"; foreach $measurement ( keys %{$weather_archive{$year}{$month}{$d +ay}} ) { print "$measurement: $weather_archive{$year}{$month}{$day}{$m +easurement}\n"; } } } }
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:

$weather_archive{$year}{$month}{$day}{$var} = $value;
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.

Make sense?

--Rhys


In reply to Re^5: 3D array of hashes by Rhys
in thread 3D array of hashes by ManyCrows

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.