ManyCrows has asked for the wisdom of the Perl Monks concerning the following question:

Say you want to work with weather data, so you might want these variables:

%day: weather data from a single day @month: array of %day hashes @season: array of @month arrays @year: array of @season arrays
What's the best way to build and print these data structures?

Replies are listed 'Best First'.
Re: 3D array of hashes
by davido (Cardinal) on Oct 08, 2003 at 21:07 UTC
    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

      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
Re: 3D array of hashes
by snax (Hermit) on Oct 08, 2003 at 20:38 UTC
    Check out perlref for clues on how to use references and anonymous data structures. Lots of good info there.

    Or, wait a bit and someone will undoubtedly serve you some fish right here :)