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

Hello,

I would like to store values similar to:

10/01/2002 00:00:00 (a,b,c,d,e,g)
10/01/2002 00:00:10 (h,i,j)
10/01/2002 00:00:20 (p,r,n)
...
05/16/2003 15:00:00 (zz,b,n)
What is the best way to do this?

What about if this were the scenario:
10/01/2002 00:00:00 a (1,2,3)
10/01/2002 00:00:00 b (5,6,7)
10/01/2002 00:00:00 c (10,1,12)
10/01/2002 00:00:10 a (1,2)
10/01/2002 00:00:10 d (1,9)
10/01/2002 00:00:10 f (10,2)
...

05/16/2003 15:00:00 a (1)
05/16/2003 15:00:00 d (2,9,10,109,10)
Sounds like a hash of hashes to me (and the second being a hash of hashes of hashes), but can a date/time value be key fields?

thanks a million!
g

Replies are listed 'Best First'.
Re: best way to store date / values
by halley (Prior) on May 16, 2003 at 20:29 UTC

    I would recommend one of two ways to "keep" a date/time stamp, and still allow it as a key in a hash.

    One is in an ISO-like string format: "YYYY-MM-DD hh:mm:ss". The benefit to this is that it's still human-readable AND will sort properly. There's no worrying about whether Jan sorts after Feb, or what the French word for August might be. Note the biggest time units come first and the smallest come last. Use leading zeroes and 24 hours.

    The other is just as a single integer, called "epoch time", "Unix time" or "Perl time." It's what time() returns. Zero refers to the time at 1970-01-01 00:00:00 GMT. It still works as hash keys, still sorts (but numerically, not ascibetically), it's still printable, but it's easier for computers to deal with later. The drawback is that it supports little more than one century (1970 +- 60ish years) in range if using a signed 32 bit value.

    --
    [ e d @ h a l l e y . c c ]

Re: best way to store date / values
by cciulla (Friar) on May 16, 2003 at 20:30 UTC

    For all of your scenarios, a hash looks like your best bet.

    Perl doesn't see "10/01/2002 00:00:00" as being any more or less special than "foo", "bar", or "baz" -- it's just a scalar.

    Update: Boy, that halley's fast. Ditto on what he said.

Re: best way to store date / values
by Anonymous Monk on May 16, 2003 at 20:37 UTC
    Perl is pretty flexible about what a hash key can be...in your case, the date-time stamp is really just a string, and this is perfectly fine as a key. As to "what is the best way to do this?" it depends on how deeply you want to parse the strings. My personal recommendation is to keep the data structure as simple as possible (hashes of hashes of hashes and beyond for such a simple application are overkill) to allow convenient access to bits of data. A simple example scheme would be to have the date-time stamps as keys pointing to a reference to an anonymous array on which you push as elements everything else:

    %superhash = (some_stamp => [a]);
    push @{$superhash{$some_stamp}}, 'b'; # etc.