Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^5: 3D array of hashes

by Rhys (Pilgrim)
on Oct 08, 2003 at 22:12 UTC ( #297759=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Re: Re: 3D array of hashes
in thread 3D array of hashes

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?


Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: 3D array of hashes
by ManyCrows (Novice) on Oct 08, 2003 at 22:36 UTC

    Rhys, that makes perfect sense. I was making my life difficult by not storing the data in the format I wanted to read it in. Duh!

    I'll be studying perlref and perlreftut hard until I feel like I'm ready for the exam at the end of the semester. ;-)

    Thanks davido and snax!

      :) I only made the comment because it seemed that you were thoughtful enough to actually care to learn the whys and not just the hows. Glad to see my humor/advice wasn't wasted. Honestly, digging into the perldocs, though intimidating at first (certanly was for me), will really open your eyes to the Perl way. The perldocs are an important part of separating the script kiddies from the determined seekers of wisdom.

      One of the best things I ever did toward learning what little I already know was to load the docs into my PDA in HTML format so that I could use it as a digital reference book. Somehow that works better for me than sitting at the notebook reading them. I refer to them often; at least as often as the Camel book.

      References are also intimidating at first. I had enough background in C (with pointers) to catch the general concepts quickly, but I'm still all thumbs with things like creating objects. But hopefully a little more fiddling and reading, and I'll gain some better perspective in that area as well.

      Keep up the learning process. Though I can't speak for the Monastery, I can speak for myself in saying I always welcome questions that are accompanied by an actual desire to understand the subject, and that make me think. To that end, keep them coming. :)


      "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://297759]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2020-11-24 18:48 GMT
Find Nodes?
    Voting Booth?

    No recent polls found