Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date sorting in Perl 1

Status
Not open for further replies.

hallm

Programmer
Jun 11, 2000
159
US
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?
 
There are a few possible approaches to your problem......<br><br>You could go to <i><A HREF=" TARGET="_new"> and get the DateManip module.&nbsp;&nbsp;That module has some useful methods.<br><br>or<br><br>You could generate integer date values using the Perl <i>time</i> function and store the integer value in your database.&nbsp;&nbsp;This would make them easy to play with and/or sort like this.....<br><b><br>sub numerically { $a &lt;=&gt; $b; }</b> # a sorting sub straight from the camel book<br># get your integer dates from your database in a list (@dates), then...<br><b>@sortedDates = sort numerically (@dates);<br>foreach $d (@sortedDates)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;$formatted = localtime($d);<br>&nbsp;&nbsp;&nbsp;&nbsp;# use your formatted date as you want<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br></b><br><br>Since the <i>time</i> function returns the date as 'the number of non-leap seconds since January 1, 1970, UTC.&quot;, ( 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.<br><br>' Hope this helps.. <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top