http://www.perlmonks.org?node_id=1156880

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
    As the English abbreviations are regular, it might be easier to define them as
    $month_names{'English abbr'} = [ map substr($_, 0, 3), @{ $month_names{English} } ];

    Also note that this solution is not universal. It breaks when you add for example Czech and Croatian:

      Croatian => [qw[ Siječanj Veljača Ožujak Travanj Svibanj Lipanj Srpanj Kolovoz Rujan Listopad Studeni Prosinac ]],
      Czech    => [qw[ leden únor březen duben květen červen červenec srpen září říjen listopad prosinec ]],
    # ...
    say get_month_number('listopad');  # 10 or 11?
    

    Update: Fixed encoding.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      choroba, while substr might appear easier, it is not shorter. Also, I don't use substr enough to remember it is in the toolbox.

      As for the Croatian and Czech month name position problem and if I were to have to choose between them, I would choose to add Czech since it has almost twice the amount of speakers then Croatian. I can't think of an easy way to fix the problem. I could add something where the user is given the option to add problematic languages if needed.

      get_month_number('listopad', {'Czech or Croatian' => 'Czech'});

      Other than that, I'm not sure.

      No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
      Lady Aleena
Re: Getting numeric month for comparisons
by morgon (Priest) on Mar 05, 2016 at 18:10 UTC
    I would build the %month-hash like this:
    my %month; for my $language (keys %month_names) { my $n=0; $month{$_} = $month{uc $_} = $month{lc $_} = $n++ for @{$month_names +{$language}}; }
    This way you don't need the @month_numbers-array at all and for my taste it it clearer.

Re: Getting numeric month for comparisons
by poj (Abbot) on Mar 05, 2016 at 18:21 UTC

    You could make the input case insensitive

    my %months; for my $language (keys %month_names) { @months{map(lc $_, @{$month_names{$language}})} = @month_numbers; } sub get_month_number { my $month = lc shift; return $months{$month}; }
Re: Getting numeric month for comparisons
by AnomalousMonk (Archbishop) on Mar 05, 2016 at 21:21 UTC
    'Italian' => [qw(gennaio febbraio marzo maggio giugno luglio agosto settembre ottobre novembre dicembre)],

    Only eleven months in the Italian calendar?!?


    Give a man a fish:  <%-{-{-{-<

      It's easy to spot that aprile is missing. It would be much harder to find the missing one in Croatian.
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      yes, is new economic measure aimed to reduce the public debt..

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.