in reply to 3D array of hashes

Actually I would want the flexibility of hashes instead of arrays. With hashes, you don't need 2004 elements to represent the year as a human-readable number. The 14th day can be called '14' instead of '13' (arrays are zero-indexed). And months could be expressed as either their numeric representation or their actual name. In addition to that, a hash would make it easier to put season breaks partway through months.
$weather_archive{'2003'}{'Spring'}{'Apr'}{'15'}{'High'} = '70f'; $weather_archive{'2003'}{'Spring'}{'Apr'}{'15'}{'Low'} = '43f';
Or like this:
%weather_archive = ( 2003 => { Spring => { April => { 15 => { High => '70f', Low => '43f', Wind => 'NW15kt', Bar => '29.85+' } } }, 2004 => ......, );
With that info to mull over in your mind, I wouldn't take it much further without consulting perlref and perlreftut. The latter is really a great place to start planting seeds of understanding of complex data structures.

Dave


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

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

    Ok, that makes a lot of sense. So, let's say you needed to build up that complex data structure from a simple data source that looks more or less like an array of hashes, with each hash looking like this:

    %hash = { Date => "04/15/2003", High => "70f", Low => "43f", Wind => "NW15kt", Bar => "29.85+", }

    How would you do it?

      I would first take that date and split it into its components:
      my ($month, $day, $year) = split /\//, $hash{Date};
      I would then use some sort of logic or lookup table to decide if $month = 4 and $day = 15 is spring, summer, fall, or winter. Then once I had decided that, I would drop it all into place as described in my previous post.

      But the more I think of it, the more I wonder if it's such a good idea to subdivide by season. It might be easier to work with if you make the season simply another element within the structure contained in the day. In other words,....

      my $season = get_season( $month, $day ); $archive{2003}{4}{15} = ( High => $hash{High}, Low => $hash{Low}, Wind => $hash{Wind}, Bar => $hash{Bar}, Seas => $season );
      You'll have to think through how to implement get_season().
      This makes it easier to find dates without having to go through the mentally challenging exercise of figuring out what season that date falls in before you can find it again. If you ever prefer to sort by season by date, you can just write a sort routine to handle that need.

      What made me first think of putting season as a field within the archive for that date is thinking back to my days as a retail buyer, where we dealt in two seasons per year, but advertised in four seasons. So it was always a mental exercise to remember that even though to me July is the beginning of the fall season, to the public July is summer, which isn't a season to a retailer.


      Dave


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

        Hmm, I'm realizing that I'm not being entirely clear.

        Say you don't have data for *every* (or every month, year, etc), so you only want those days/months/years in %weather_archive for which you have data.

        So, I'm assuming you'll want to iterate through the original array of hashes, pulling out unique values for days/months/years.

        To simplify this, let's say I've munged the data a bit, and now my hashes looks like this:

        %weather_day = ( Year => "2003", Month => "April", Day => "15", High => "73f", Low => "49f" ); push (@weather_days, %weather_day);

        How can I extract the unique year/month/day values, and then programmatically build up the %weather_archive hash?

        Once I have %weather_archive, how can I print it so that it looks something like this?

        2003 April 15 High: 73f Low: 49f