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

Hi Monks,
I running a query to a database and after getting all the results back I am left with a problem trying to sort trough the values for displaying the data, since it's stored in different fields in the data base.
After the query, month (Field Name for the Month in DB table) gets stored in $month.
day (Field Name for the Day in DB table) gets stored in $day.
year (Field Name for the Year in DB table) gets stored in $year.
Now my problem is that I am trying to sort these values and format the dates properly like 11/25/2004 at the end, any suggestions?
I need to format the data and display an option of displaying in numeric order like:

<loop will be here>{ print “$day/$month/$year<br>”; }

Result prints:
8/8/2004
1/13/2002
1/12/2002
2/10/2002
11/1/2000

Thanks for the help!

Replies are listed 'Best First'.
Re: Sorting Question
by hardburn (Abbot) on Aug 04, 2004 at 13:49 UTC

    Two possibilities for sorting:

    1. Lookup how your database does SORT in SQL
    2. Load all the data into an array and sort it from within Perl (which will take more memory)

    For formatting the dates, I prefer the DateTime family of modules.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: Sorting Question
by EdwardG (Vicar) on Aug 04, 2004 at 13:52 UTC

    If you concatenate the date parts as YYYY/MM/DD they will then be easily sortable. This requires you to ensure that each element of the date has the right number of digits, for example "08" instead of "8".

    Another option would be to convert these dates into something like Date::Simple and then sort them.

     

Re: Sorting Question
by rbi (Monk) on Aug 04, 2004 at 13:55 UTC
    If I understand what you mean, you can build a date string concatenating year, month and day. then you can sort it and split it back to year, month and day before printout.
Re: Sorting Question
by davido (Cardinal) on Aug 04, 2004 at 16:00 UTC

    You probably have your reasons, so I shouldn't ask. But just in case, I was wondering if you had a really good reason for storing YEAR, MONTH and DAY as separate fields in the database, instead of being stored in a date format recognized by your database.

    Most DB's can sort dates natively, and for those that can't, Perl has a proliferation of date modules that simplify the process. ...but all of these methods work best with a complete date.


    Dave

Re: Sorting Question
by dws (Chancellor) on Aug 04, 2004 at 15:46 UTC

    ... I am left with a problem trying to sort trough the values for displaying the data, since it's stored in different fields in the data base.

    Assuming you're using a SQL database, this is what the ORDER BY clause in SELECT queries is for. Something like

    ORDER BY year, month, day

    might work for you.

Re: Sorting Question
by johnnywang (Priest) on Aug 04, 2004 at 17:29 UTC
    Assuming you are storing your records as an array of references to your recrods, i.e.,
    my @records = (['2004','4','23','other part of recrods'], ['2002','2','12','some other stuff']);
    You can then sort them as:
    @records = sort {$a->[0]<=>$b->[0] || $a->[1]<=>$b->[1] || $a->[2] <=> $b->[2]} @records;
    Check the Cook book's array chapter for sorting methods.