use Date::Parse;
my @records_sorted = sort {
# extracting date fields from the lines
my ($date_str1) = split /\s+/, $a, 2;
my ($date_str2) = split /\s+/, $b, 2;
# parsing the date strings into numeric timestamps
my $date1 = str2time($date_str1);
my $date2 = str2time($date_str2);
my $cmp;
# check for parse errors
if (not defined $date1) {
warn("Failed to parse date: $date_str1");
$cmp = -1;
}
elsif (not defined $date2) {
warn("Failed to parse date: $date_str2");
$cmp = 1;
}
else { # success, compare dates numerically
$cmp = $date1 <=> $date2
}
$cmp
} @records
Of course you need to have the Date::Parse module installed.
Also note that this code assumes that your date fields are the very first thing on each line and have a whitespace character after them.
Installing packages is always a problem. Yes using underscore was a TYPO.
Second, do you really have one timezone given as +08:00 (with colon) and other as +0000 (without colon)? Or was it a typo?
>>< I even dont know what these are after the DATES (after T) . I just need to sort the documents according to their time they were created and time is given in this format
Without the module, you have a problem. You'd have to parse the dates, which would be pretty easy without the timezones. With them, you'd have to be able to add and subtract dates, which is crazily complicated. It's definitely easier to install a module, even if you only have FTP access, trust me, I did both.
But if you can live without the timezones in regard, that is, if you're OK that you'll list 2010-01-03 14:25 CET later than 2010-01-03 13:40 UTC, then it's you can do something like this:
Code:
# from 2006-02-05T06:56:00-05:00
# to 2006_02_05_06_56_00
sub numify_date {
my ($date_str) = @_;
my $date_num =~ s/\D+/_/g;
return substr($date_num, 19)
}
my @records_sorted = sort {
# extracting date fields from the lines
my ($date_str1) = split /\s+/, $a, 2;
my ($date_str2) = split /\s+/, $b, 2;
# parsing the date strings into numeric expressions
my $date1 = numify_date($date_str1);
my $date2 = numify_date($date_str2);
$date1 <=> $date2
} @records
Without the module, you have a problem. You'd have to parse the dates, which would be pretty easy without the timezones. With them, you'd have to be able to add and subtract dates, which is crazily complicated. It's definitely easier to install a module, even if you only have FTP access, trust me, I did both.
But if you can live without the timezones in regard, that is, if you're OK that you'll list 2010-01-03 14:25 CET later than 2010-01-03 13:40 UTC, then it's you can do something like this:
Code:
# from 2006-02-05T06:56:00-05:00
# to 2006_02_05_06_56_00
sub numify_date {
my ($date_str) = @_;
my $date_num =~ s/\D+/_/g;
return substr($date_num, 19)
}
my @records_sorted = sort {
# extracting date fields from the lines
my ($date_str1) = split /\s+/, $a, 2;
my ($date_str2) = split /\s+/, $b, 2;
# parsing the date strings into numeric expressions
my $date1 = numify_date($date_str1);
my $date2 = numify_date($date_str2);
$date1 <=> $date2
} @records
Thanks but I could not understand the line "extracting date fields from the lines". Which line do u mean here.
Scenario is that I have an Array of dates and I have to sort them.
Thanks already for ur kind help
Sir I did debug and I removed syntax errors but still even after your corrected version, its not printing well the sorted record. I want the records be printed date wise ... Publish the one first with OLD DATE.
But its giving kind of mix results i.E. Unsorted yet.
Above is a portion of the array which is sorted. Below is the code
Code:
################ sorting records with date ##############################"
sub numify_date {
my ($date_str) = @_;
my $date_num =~ s/\D+//g;
return substr($date_num, 0, 19);
}
my @records_sorted = sort {
# extracting date fields from the lines
my ($date_str1) = split /\s+/, $a, 2;
my ($date_str2) = split /\s+/, $b, 2;
# parsing the date strings into numeric expressions
my $date1 = numify_date($date_str1);
my $date2 = numify_date($date_str2);
$date1 <=> $date2
} @BIG;
Bookmarks