Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Date sorting in Perl

Date sorting in Perl

Date sorting in Perl

I'm working on a simple database in perl.  In my viewer script (This is the script that will list a summary of my content), I want the content to be listed in descending order by date with only the last 30 days to be present.  I'm using an array to read the data from my text file.  I'm a novice perl programmer and I need just a little help.  Any ideas?

RE: Date sorting in Perl

There are a few possible approaches to your problem......

You could go to http://www.cpan.org/ and get the DateManip module.  That module has some useful methods.


You could generate integer date values using the Perl time function and store the integer value in your database.  This would make them easy to play with and/or sort like this.....

sub numerically { $a <=> $b; }
# a sorting sub straight from the camel book
# get your integer dates from your database in a list (@dates), then...
@sortedDates = sort numerically (@dates);
foreach $d (@sortedDates)
    $formatted = localtime($d);
    # use your formatted date as you want

Since the time function returns the date as 'the number of non-leap seconds since January 1, 1970, UTC.", ( again, from the camel book), you can discard the old dates as those with date integer values less than the current date value minus one month's worth of seconds.

' Hope this helps..

keep the rudder amid ship and beware the odd typo

RE: Date sorting in Perl

Thank you for your help.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close