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

Hello all,

I a have a script that allows me to enter Expo events which are then printed to an html file. I want them to be sorted by the events start date. Here is how the data is stored in the flat db file.

Macworld Expo|1/7|1/12|San Francisco|CA|http://www.macworldexpo.com

So events need to be sorted by the field "1/7" or jan 7th. Dates after 1/7 would be sorted next in the list. I tried doing this by changing the dates to numbers -- for ex:
1/7 becomes 172001 and 11/17 becomes 11172001 but of course this doesn't work out logically for sorting.

Any suggestions?

Edited 2001-09-07 by Ovid

Replies are listed 'Best First'.
(Ovid) Re: Sorting Data by Date
by Ovid (Cardinal) on Sep 07, 2001 at 22:38 UTC

    As mentioned previously, using sprintf will allow you to format your data correctly. As for the sort, the following would work, if I understood your requirements correctly, but I seriously doubt that it's the most efficient way to do things.

    use strict; use Data::Dumper; my @expos; chomp( @expos = <DATA> ); @expos = map { "$_->[0]|$_->[1]/$_->[2]|$_->[3]/".(join "|", @{$_}[4. +.$#$_]) } sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } map { [ split '[|/]', $_, 8 ] } @expos; print Dumper \@expos; __DATA__ Macworld Expo|2/7|1/12|San Francisco|CA|http://www.macworldexpo.com Another Expo|1/27|2/5|San Francisco|CA|http://www.anotherexpo.com Some Expo|1/6|1/12|San Francisco|CA|http://www.macworldexpo.com

    Generally, one should avoid a Schwartzian transform if the you do not have expensive data munging, but it's the first thing that occurred to me and I think they look rather cool :)

    Cheers,
    Ovid

    Vote for paco!

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Sorting Data by Date (boo)
by boo_radley (Parson) on Sep 07, 2001 at 22:18 UTC
    use sprintf to pad out the day and month to 2 digits.
    perl -e "sprintf('%02d',1)"

    updated

      Can you explain what "printf('%02d', 1)" and how would I implement this code onto my list of data? I tried using is like this: $date = printf ("%02d", $start_date); it didn't work using the syntax you gave me.
        You're confusing printf, which prints, with sprintf, which doesn't print, but returns a string. Same arguments, different side-effects and return values.

Re: Sorting Data by Date
by stefp (Vicar) on Sep 07, 2001 at 22:28 UTC
    I am keeping with you approach of sorting strings using lexicographic order and I am supposing that you know how to parse the dates. You need to use the format %02s in sprintf so that the first day of the week and the first month of the year are represented with 2 digits. You can use a subroutine like date2str below and sort the resulting strings.
    sub date2str { my ( $month, $day, $year, $rest } = @_; sprintf( "%04d%02d%02d%s", $year, $month, $day, $rest ); }

    -- stefp

      thanks for the reply. With your above code -- would this process the "|" data in the flat db file? If so it doesn't look like it's taking that into account. Also right now I just have one field for entering the date - I could seperate them out to different fields -- each with it's own scalar value. In addition when you say $rest do you mean the rest of the data in the "|" db file?
Re: Sorting Data by Date
by John M. Dlugosz (Monsignor) on Sep 08, 2001 at 00:59 UTC
    Use an order of the parts that does work out logically. E.g. "2001-12-30" or "2002-01-02".