Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

(Ovid) Re: sorting by quarterly calendar

by Ovid (Cardinal)
on Aug 27, 2001 at 20:12 UTC ( [id://108163]=note: print w/replies, xml ) Need Help??


in reply to sorting by quarterly calendar

I think the following would work. I didn't try to adjust for null dates as I have no idea what your data format is. The snippet sorts by year, then by quarter, then by day.

use strict; use warnings; use Data::Dumper; my @sorted = map { [ $_->[0], join( '/', @{$_}[1..3] ), $_->[4] ] } sort { $a->[3] <=> $b->[3] || $a->[0] <=> $b->[0] || $a-> +[2] <=> $b->[2] } map { [ get_quarter( $_->[0] ), split( '/', $_->[0] ), $ +_->[1] ] } map { [ split( /\s/, $_, 2 ) ] } <DATA>; chomp @$_ foreach @sorted; print Dumper \@sorted; sub get_quarter { my @quarters = qw( 3 3 4 4 4 1 1 1 2 2 2 3 ); my ( $month ) = (split'/',$_[0])[0]; $quarters[ $month - 1 ]; } __DATA__ 10/23/2000 data1 foo bar baz 11/19/2000 data2 some stuff 08/11/2001 data3 what the heck? 04/30/2001 data4 this is getting dull 01/01/2001 data5 ??

Here's what it does:

  • It splits the data on whitespace. That leaves the date in the first field. Note the optional third parameter to split which ensures that all data after the date is left untouched.
  • It passes the first field to the sub get_quarter() and returns the quarter, along with the date split on the delimeter and the remaining data.
  • It sorts on year, then quarter, then day.
  • It puts the data back together.

Each array ref is going to have the quarter as the first element, the date as the second and the data as the third.

You're going to have to play with the above snippet as you need to account for null dates. Further, I really didn't test this for multiple quarters spanning multiple years (I assumed you'd have one fiscal year of data at a time).

Cheers,
Ovid

Vote for paco!

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-03-28 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found