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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Getting numeric month for comparisons
by choroba (Cardinal) on Mar 05, 2016 at 18:31 UTC | |
by Lady_Aleena (Priest) on Mar 06, 2016 at 13:30 UTC | |
Re: Getting numeric month for comparisons
by morgon (Priest) on Mar 05, 2016 at 18:10 UTC | |
Re: Getting numeric month for comparisons
by poj (Abbot) on Mar 05, 2016 at 18:21 UTC | |
Re: Getting numeric month for comparisons
by AnomalousMonk (Archbishop) on Mar 05, 2016 at 21:21 UTC | |
by choroba (Cardinal) on Mar 05, 2016 at 21:25 UTC | |
by Discipulus (Canon) on Mar 09, 2016 at 08:42 UTC |
Back to
Cool Uses for Perl