Reputation: 97
I need help sorting in perl. I have an array of dates in this format. DD-MMM-YY. Example 19-FEB-12.
I have spent quite a lot of time already, but couldn't get it work. I'm very new to perl as well. Any amount of help is appreciated. Thanks!!
Upvotes: 0
Views: 4050
Reputation: 439
Totally from scratch:
Printed Output:
Unsorted:
14-OCT-06
15-OCT-06
13-OCT-06
19-FEB-12
29-DEC-02
15-JAN-02
Sorted (Least recent to most recent):
15-JAN-02
29-DEC-02
13-OCT-06
14-OCT-06
15-OCT-06
19-FEB-12
Code:
#!C:\Perl64
#Input Strings
@inputs = ('14-OCT-06','15-OCT-06','13-OCT-06', '19-FEB-12', '29-DEC-02', '15-JAN-02');
print "Unsorted:\n";
foreach(@inputs){
print $_."\n";
}
# Hash for Month : Number
%months = ('JAN' => '01',
'FEB' => '02',
'MAR' => '03',
'APR' => '04',
'MAY' => '05',
'JUN' => '06',
'JUL' => '07',
'AUG' => '08',
'SEP' => '09',
'OCT' => '10',
'NOV' => '11',
'DEC' => '12');
# Hash for Number : Month
%revmonths = ('01'=>'JAN',
'02' => 'FEB',
'03' => 'MAR',
'04' => 'APR',
'05' => 'MAY',
'06' => 'JUN',
'07' => 'JUL',
'08' => 'AUG',
'09' => 'SEP',
'10' => 'OCT',
'11' => 'NOV',
'12' => 'DEC');
# Rearrange the order to 'Year-Month-Day'
@dates = ();
foreach(@inputs){
my @split = split('-',$_);
my @rearranged = reverse(@split);
@rearranged[1] = $months{$rearranged[1]};
push(@dates, \@rearranged);
}
# Sort based on these three fields
@sorted = sort { $a->[2] <=> $b->[2] } @dates;
@sorted = sort { $a->[1] <=> $b->[1] } @sorted;
@sorted = sort { $a->[0] <=> $b->[0] } @sorted;
# Replace Month Number with Month name
$size = @sorted;
for $counter (0..$size-1){
my $ref = $sorted[$counter];
my @array = @$ref;
my $num = $array[1];
$array[1] = $revmonths{$array[1]};
my @array = reverse(@array);
$sorted[$counter] = \@array;
}
print "\nSorted (Least recent to most recent):\n";
foreach(@sorted){
my @temp = @$_;
my $day = $temp[0];
my $month = $temp[1];
my $year = $temp[2];
print $day."-".$month."-".$year;
print "\n";
}
Upvotes: -2
Reputation: 604
Using DateTime module; or use sort command with month option (*nix); or transform dd-mmm-yyyy to yyyymmdd and then sort;
Upvotes: 0
Reputation: 29635
Here's a possible way to do this using only basic Perl (no modules):
#! perl -w
use strict;
my @dates = ( '19-FEB-12', '15-APR-12', '13-JAN-11' );
# map month names to numbers
my %monthnum = (
'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4,
'MAY' => 5, 'JUN' => 6, 'JUL' => 7, 'AUG' => 8,
'SEP' => 9, 'OCT' => 10, 'NOV' => 11, 'DEC' => 12
);
# sort the array using a helper function
my @sorted_dates = sort { convert_date($a) cmp convert_date($b) } @dates;
print join(", ", @sorted_dates), "\n";
# outputs: 13-JAN-11, 19-FEB-12, 15-APR-12
exit(0);
# THE END
# converts from 19-FEB-12 to 120219, for sorting
sub convert_date
{
my $d1 = shift;
my $d2;
if ($d1 =~ /(\d{2})-([A-Z]{3})-(\d{2})/)
{
$d2 = sprintf( "%02d%02d%2d", $3, $monthnum{$2}, $1 );
}
else
{
die "Unrecognized format: $d1";
}
return $d2;
}
This relies on your dates being formatted correctly, but it should be trivial to add more validation.
Upvotes: 1
Reputation: 126742
This can be done using the Time::Piece
core module's strptime
method to decode the dates and sorting them according to the resulting epoch seconds.
This program demonstrates the idea.
use strict;
use warnings;
use Time::Piece;
my @dates = <DATA>;
chomp @dates;
my @sorted = sort {
Time::Piece->strptime($a, '%d-%b-%y') <=> Time::Piece->strptime($b, '%d-%b-%y')
} @dates;
print "$_\n" for @sorted;
__DATA__
05-FEB-12
10-MAR-11
22-AUG-11
26-FEB-12
10-NOV-12
07-JUN-11
20-APR-12
19-DEC-12
17-JAN-11
25-NOV-11
28-FEB-11
04-SEP-11
03-DEC-12
16-SEP-12
31-DEC-11
08-JUN-11
22-JUN-12
02-AUG-12
23-SEP-11
14-MAY-11
output
17-JAN-11
28-FEB-11
10-MAR-11
14-MAY-11
07-JUN-11
08-JUN-11
22-AUG-11
04-SEP-11
23-SEP-11
25-NOV-11
31-DEC-11
05-FEB-12
26-FEB-12
20-APR-12
22-JUN-12
02-AUG-12
16-SEP-12
10-NOV-12
03-DEC-12
19-DEC-12
Upvotes: 10
Reputation: 7526
You could use the core module Time::Piece
to convert the DD-MMM-YY (or any input format) to an ISO 8601 form. This allows simple sorting. This example builds up an array of raw data which includes the ISO value as a sort key; sorts it; and returns the data in sorted order:
#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;
my $t;
my @data;
while (<DATA>) {
chomp;
$t = Time::Piece->strptime( $_, "%d %b %y" );
push @data, [ $t->datetime, $_ ]; #...ISO 8601 format...
}
my @sorteddata = sort { $a->[0] cmp $b->[0] } @data;
for my $value (@sorteddata) {
print $value->[1], "\n";
}
__DATA__
19 Feb 12
17 Aug 11
31 Mar 10
01 Aug 11
08 Apr 11
29 Feb 11
Upvotes: 2
Reputation: 10242
An easy way is to convert the dates to YYYYMMDD
format that can be sorted lexicographically.
Note that MM
should be the month represented as a two digit number.
Upvotes: 3