Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^2: Date plus Time sort from file

by Laurent_R (Parson)
on Aug 15, 2013 at 08:53 UTC ( #1049527=note: print w/ replies, xml ) Need Help??

in reply to Re: Date plus Time sort from file
in thread Date plus Time sort from file

Yes, I fully agree with tobyink, filtering first and then only sorting can generally improve performance dramatically because the program will have much less work to do (at least if you have many records). In this specific case, filtering on today's date offers another major advantage: since all the dates will be the same, you really have to sort on time, and this is much simpler and easier thah sorting dates in your given format.

If you had to sort on dates and then times, you would need to split the records in order to sort first the years, then the months, then the days, and then the time (or possibly apply some other type of transformation to your data). Here, since all the dates will have been filtered and will therefore be the same, a simple sort on the full record will produce the desired result.

So you basically need something as simple as this:

my @sorted_records = sort grep {/^08-14-2013/} @unsorted_records;

Of course, in real life, you probably don't want to hardcode the date (08/14/2013) in your filtering. If you want to find today's date in your specific date format, you can do something like this:

my ($day, $month, $year) = (localtime time)[3..5]; $year += 1900; $day = sprintf "%02d", $day: $month = sprintf "%02d", $month + 1; my $date = "$month-day-$year";

Or you could use one of the numerous date modules available on the CPAN. Or the strftime POSIX function illustrated above by boftx.

Comment on Re^2: Date plus Time sort from file
Select or Download Code
Re^3: Date plus Time sort from file
by boftx (Chaplain) on Aug 15, 2013 at 09:29 UTC

    Based on what was just said, my example can be simplified even further:

    # this is what should be used instead of the hard-coded value: # my $today = strftime("%m-%d-%Y",localtime()); my $today = '08-14-2013'; my @todays; for ( @orig_data ) { next unless m/^($today )/; push(@todays,$_); } # NOTE! this will reverse the order of Yes and No if they have the # same time. @todays = sort(@todays); for ( @todays ) { print "$_\n"; }

    I agree that filtering then sorting will result in (sometimes large) performance gains. In this particular case, where the unsorted array is actually records coming from a file, you only want to store the records of interest in memory instead of slurping in the entire file. RAM might be cheap, but it is a finite resource and SAs can be a nasty breed of cat. (Remember the BOFH?)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1049527]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (21)
As of 2014-10-22 13:33 GMT
Find Nodes?
    Voting Booth?

    For retirement, I am banking on:

    Results (118 votes), past polls