Perl month hash

I'm really tired of retyping this:

my $mons = {
    'Jan' => 1,
    'Feb' => 2,
    'Mar' => 3,
    'Apr' => 4,
    'May' => 5,
    'Jun' => 6,
    'Jul' => 7,
    'Aug' => 8,
    'Sep' => 9,
    'Oct' => 10,
    'Nov' => 11,
    'Dec' => 12,
};

Then access it with $mons->{Jul} to get the number. Or you can flip it to get a mapping of numbers to months:

$rev = { reverse(%$mons) };
Leave A Reply - 10 Replies
Replies
dan 2008-09-09 03:55pm - No Email - Logged IP: 71.242.248.207

me too! thanks.

RD 2008-10-23 11:47am - No Email - Logged IP: 192.251.13.62

Dude... sweet !

sakthi 2009-07-16 06:23am - No Email - Logged IP: 72.163.216.217

my %months = { '1'=>'Jan','2'=>'Feb','3'=>'Mar','4'=>'Apr','5'=>'May','6'=>'Jun','7'=>'Jul','8'=>'Aug','9'=>'Sep','10'=>'Oct','11'=>'Nov','12'=>'Dec'};

Incase, you want the reverse one with strict

Devin 2009-11-27 10:47am - No Email - Logged IP: 64.53.128.177

Thanks, just what I needed and the #2 google result for "perl month"

Hazhir 2010-03-09 05:38am - No Email - Logged IP: 193.239.153.153

thank you...

chris 2010-09-03 09:45am - No Email - Logged IP: 72.248.253.194

you, sir, just saved me 30 seconds, which i squandered thanking you.

thank you.

DUDE 2011-02-24 10:00am - No Email - Logged IP: 85.189.184.66

DUDE THE SYNTAX IS WRONG ITS () FOR HAS NOT {} ARRRGGGHHH YOU WASTED MY TIME DEBUGGING NOT helped.. BUT THANKS ANYWAY actually the months helped so i dont type tyhem in.

Jeff 2012-01-14 09:52am - No Email - Logged IP: 68.35.96.86

the code is right you just have to know how to get at the data

my %mons = ('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 $mons{'Jan'};

my $mon = {'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 $mon->{'Jan'};

THIS IS WRONG 2016-07-15 01:36pm - No Email - Logged IP: 63.236.116.94

Loser.

Matt 2017-12-20 07:22pm - No Email - Logged IP: 13.54.239.82

A decade later, search, cut and paste!

All content licensed under the Creative Commons License