First off, there are some perl modules that do all of this stuff for you. Date::Calc and Date::Manip are both good starts. (I'm sure someone will have posted links before I finish typing this.)

But, in the interest of learning a programming trick or two, though, consider doing your month math something more like:

my @months = qw(JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC); sub change_num_to_month { my $num_month = shift; return $months[$num_month - 1]; } sub add_month { my $month = shift; my $month_num = change_month_to_num($month); $month_num++; $month_num = 1 if $month_num > 12; return change_num_to_month($month_num); } # And very similarly for subtract month
Getting the reverse lookup is slightly trickier. I'd probably convert the array into a hash, something like: (I'm sure you can do better with a map, but let's go for readibilty first.)
my %month_nums; for (my $i = 0; $i <= $#months; $i++) { $month_nums{$month[$i]} = $i; } # This creates a hash of 'Jan' => 0, 'Feb' => 1, etc. sub change_month_to_num { my $month = shift; return $month_nums{$month} + 1; }
One has to be careful about the array index starting at 0, if you want your month nums to range from 1 to 12, but that's the only really tricky bit.

-- Kirby, WhitePages.com


In reply to Re: adding/subtracting calender dates by kirbyk
in thread adding/subtracting calender dates by Fuism

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.