[lug] Perl Question..
Joseph McDonald
joem at uu.net
Thu Apr 17 15:52:52 MDT 2003
On Thu, Apr 17, 2003 at 01:44:31PM -0600, Tkil wrote:
> >>>>> "Joseph" == Joseph McDonald <joem at uu.net> writes:
>
> Joseph> Not directly linux related, so hopefully I don't get flamed
> Joseph> but can anyone think of a way to sort this array into a list
> Joseph> which matches the actual calandar months? Meaning, Jan, Feb,
> Joseph> March, April ... Dec. I've been racking my brain today to no
> Joseph> avail:
>
> How did you get this list?
>
> I ask because I'm curious why you are wanting to do this particular
> task. My guess is that you have a series of dates, and you want to
> put them into chronologic order. If so, consider using Date::Parse or
> one of the other modules; it will likely be less fragile than any
> hand-made solution.
Well, that is a good point. A seperate process creates monthly
reporting files in a directory and it names them something like
this: '$month.$year.html'. I was initially working on a predefined
hash, but I ended up reading up on sort() and came up with this:
my @files = <*.html>;
my @var = sort month_year_sort @files;
for (@var) {
chomp;
my ($month, $year, $ext) = split /\./;
print "<a href=\"$_\">$month -- $year</A><BR>\n";
}
sub month_year_sort
{
my $i = 1;
my $status = 1;
my $int_a = int_month($a);
my $int_b = int_month($b);
$a =~ /\w+\.(\d{4})\.html/;
$a_year = $1;
$b =~ /\w+\.(\d{4})\.html/;
$b_year = $1;
return $a_year <=> $b_year || $int_a <=> $int_b;
}
sub int_month
{
@months = qw ( Jan Feb Mar Apr May June Jul Aug Sep Oct Nov Dec );
my $i = 1;
foreach $month ( @months ) {
return $i if $_[0] =~ /^$month/;
$i++;
}
}
--joey
More information about the LUG
mailing list