Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) 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:

$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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found