I wrote this little script to get numeric months for comparisons. The input can be in initial uppercase, all uppercase, and all lowercase. I added a few other languages for fun.
#!/usr/bin/perl
use strict;
use warnings;
my @month_numbers = (1..12);
my %month_names = (
'English' => [qw(January February March April May June July Aug
+ust Spetember October November December)],
'English abbr' => [qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Now De
+c)],
'Dutch' => [qw(januari februari maart april mei juni juli aug
+ustus september oktober november december)],
'French' => [qw(janvier février mars avril mai juin juillet ao
+űt septembre octobre novembre décembre)],
'German' => [qw(Januar Februar März April Mai Juni Juli August
+ September Oktober November Dezember)],
'Greek' => [qw(Ianuários Fevruários Mártios Aprílios Máios Iú
+nios Iúlios Avghustos Septémvrios Októvrios Noémvrios Thekémvrios)],
'Italian' => [qw(gennaio febbraio marzo aprile maggio giugno lu
+glio agosto settembre ottobre novembre dicembre)],
'Spanish' => [qw(enero febrero marzo abril mayo junio julio ago
+sto septiembre octubre noviembre diciembre)],
);
my %months;
for my $language (keys %month_names) {
@months{@{$month_names{$language}}} = @month_numbers;
@months{map(uc $_, @{$month_names{$language}})} = @month_numbers;
@months{map(lc $_, @{$month_names{$language}})} = @month_numbers;
}
sub get_month_number {
my $month = shift;
return $months{$month};
}
So, if you want the month number for July, you use it like the following.
print get_month_number('July');
The above will return 7.
Now you can put dates in the right order. I'll be modularizing this shortly. 8)
Updates
Code simplified from morgon's (no @month_numbers array) and poj's (case insensitive input) suggestions.
my %months;
for my $language (keys %month_names) {
my $number = 0;
$months{lc $_} = ++$number for @{$month_names{$language}};
}
sub get_month_number {
my $month = lc shift;
return $months{$month};
}
Added aprile to Italian list of months from AnomalousMonk.
No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.