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

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?

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

        Given the above snippets you provided, you would do this:
        foreach $date ( @weather_days ) { $weather_archive{$date->{Year}}{$date->{Month}}{$date->{Day}} = ( $date->{High}, $date->{Low}, $date->{Season}, $date->{Wind}, $date->{Bar} ); }
        For that to work right, you need to not be pushing %hash onto @array (that will flatten hash into a list) but rather push @array, {%hash}; (push an anonymous hash reference).

        I'm sure that my snippet can be done with a map too but this is good enough. Oh, and that's the part that perlreftut would give you a better grasp on. Go ahead and read perlreftut and perlref as mentioned earlier so that you won't flunk the exam at the end of the semester. Producing working code today, and understanding the concepts behind it are two different things. This is something that you will use a lot once you understand the concepts. Failing to dig until the concept sinks in is going to cripple your progress.


        Dave


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