in reply to Re: Hash Math
in thread Hash Math

I have an array for just the dates, I actually zipped together my @dates array and my @tifs array to create the %hash. So if it would be easier doing the math in the array, that's fine, since I have the elements in the hash, I can always reference the hash later to get the tif corresponding to the date.

So, would I just use each to iterate through my date array to do the necessary comparisons?

Replies are listed 'Best First'.
Re^3: Hash Math
by Cristoforo (Curate) on Jun 10, 2014 at 02:16 UTC
    Here is a way to get the difference between the dates in days from an array of dates. (Note that the dates are sorted. If your array isn't sorted you would have some negative differences).
    #!/usr/bin/perl use strict; use warnings; use Time::Piece; my @date = ( '2014-06-01', '2014-06-02', '2014-06-03', '2014-06-04' ); for my $i (0 .. $#date-1) { my $d1 = Time::Piece->strptime($date[$i], '%Y-%m-%d'); for my $j ($i+1 .. $#date) { my $d2 = Time::Piece->strptime($date[$j], '%Y-%m-%d'); my $diff = $d2 - $d1; print $d1->ymd, " and ", $d2->ymd, " is ", $diff->days, "\n"; } }
    Prints
    2014-06-01 and 2014-06-02 is 1 2014-06-01 and 2014-06-03 is 2 2014-06-01 and 2014-06-04 is 3 2014-06-02 and 2014-06-03 is 1 2014-06-02 and 2014-06-04 is 2 2014-06-03 and 2014-06-04 is 1
Re^3: Hash Math
by perlfan (Parson) on Jun 10, 2014 at 14:25 UTC
    Tie::Hash::Indexed maintains an ordered hash. Also, use each only if you're aware of the fact that breaking an iteration partly through the hash means that subsequent iterations will start where it left off. You may be safer with the foreach/keys idiom.

    It may be worth mentioning PDL if you're going to be doing math stuffs with complex data structures. But it might be a bit overkill for your application.