xorl has asked for the wisdom of the Perl Monks concerning the following question: (dates and times)

I'm sure this is a dumb question, but I can't seem to find the answer. I have a variable that contains a month name i.e. "October" I want to change that to the number i.e. "10". Is there an easy way of doing this. My only thought was
if ($var eq "January") { $var=1; } elsif ($var eq "Feburary") ...
but this seems rather long and tedious.
Thanks!

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Convert month name to number
by japhy (Canon) on Jul 11, 2001 at 00:24 UTC
    You want a hash.
    %mon2num = qw( jan 1 feb 2 mar 3 apr 4 may 5 jun 6 jul 7 aug 8 sep 9 oct 10 nov 11 dec 12 ); print $mon2num{ lc substr($month, 0, 3) };
    That works for "JANUARY", "Mar", "decemb.", "oct", and many other variants.
      Great implementation japhy! A word of warning to the wise that if you plan on using this with POSIX mktime and friends you'll want to subtract 1 from the month number (e.g. January = 0, December = 11).
Re: Convert month name to number
by particle (Vicar) on Jul 11, 2001 at 00:51 UTC