C
cartercc
I have a series of date related values, such as 01/2007, 02/2007,
03/2008, 04/2006, etc. These values represent a column in various
files that range from several dozen rows deep to almost 1M rows deep.
My job is to create reports from a collection of these types of files.
I create a number of refs to hashes that have the general appearance
of this:
$h{$k1}{$k2}{$k3} => data (generally but not always a simple count).
I write to an outfile generally like this:
foreach $k1 (sort keys %h) {
foreach $k2 (sort keys %{$h{$k1}}) (
print OUTFILE " <k1>$k1</k1> <k2>$k2</k2> <val>$h{$k1}{$k2}</val>
\n";
}
}
When I run this, it works perfectly, sorting the date values into
perfect numerical order. When something's so perfect, you know it's
wrong!
Here's the problem: the ordering of the dates isn't numerical, the
proper order is- 03/2005, 04/2005 ... 01/2006, 02/2006
03/2006, 04/2006 ... 01/2007, 02/2007
03/2007, 04/2007 ... 01/2008, 02/2008
Here's another view of the problem - when I print data for a year, the
ordering is:
03/07, 04/07, 05/07 ... 01/07, 02/07
What I would like to do is overload the sort operator (call it
'sort_y') to sort in this non-numerical order. Can this be done? Can
it be done perhaps in C and compiled to run in Perl? Can it be done
algorithmetically by passing sort a function of some kind? I've tried
this, and the logic is very clumsy and full of stupid relational
operators and elsifs.
The former solution was to copy the data into Excel and manually cut
and paste the columns in the correct order. Some of these reports are
enormous (25 cols by 2500 rows) and I don't want to do this. This took
a lot of time and was very much error prone.
Thanks, CC.
03/2008, 04/2006, etc. These values represent a column in various
files that range from several dozen rows deep to almost 1M rows deep.
My job is to create reports from a collection of these types of files.
I create a number of refs to hashes that have the general appearance
of this:
$h{$k1}{$k2}{$k3} => data (generally but not always a simple count).
I write to an outfile generally like this:
foreach $k1 (sort keys %h) {
foreach $k2 (sort keys %{$h{$k1}}) (
print OUTFILE " <k1>$k1</k1> <k2>$k2</k2> <val>$h{$k1}{$k2}</val>
\n";
}
}
When I run this, it works perfectly, sorting the date values into
perfect numerical order. When something's so perfect, you know it's
wrong!
Here's the problem: the ordering of the dates isn't numerical, the
proper order is- 03/2005, 04/2005 ... 01/2006, 02/2006
03/2006, 04/2006 ... 01/2007, 02/2007
03/2007, 04/2007 ... 01/2008, 02/2008
Here's another view of the problem - when I print data for a year, the
ordering is:
03/07, 04/07, 05/07 ... 01/07, 02/07
What I would like to do is overload the sort operator (call it
'sort_y') to sort in this non-numerical order. Can this be done? Can
it be done perhaps in C and compiled to run in Perl? Can it be done
algorithmetically by passing sort a function of some kind? I've tried
this, and the logic is very clumsy and full of stupid relational
operators and elsifs.
The former solution was to copy the data into Excel and manually cut
and paste the columns in the correct order. Some of these reports are
enormous (25 cols by 2500 rows) and I don't want to do this. This took
a lot of time and was very much error prone.
Thanks, CC.