Hi experts, I have created my own program to add and subtract (in this example Im subtraction 7 days from current day so basically a week ago) calendar dates. Is there an easier way to do this without having to update the code (in my validate_years and validate_month subs). It works but Im still unconfident about the outcome. Sorry the code is so long. Any advice would be appreciated. Thanks in advance... Fue
#!/usr/bin/perl #Variables for dates $year = qx(date +%Y); $month = qx(date +%b); $date = qx(date +%d); $date2 = $date; $month = uc($month); chop($month); chop($date); chop($date2); chop($year); #Opens template and write to SAS file to use die("Cannot open daily_pull_temp.sas to write to.") unless(open(WRITE, ">daily_pull_temp.sas")); die("Cannot open daily_pull.sas to read from.") unless(open(GETDATA, "<daily_pull.sas")); while($get = <GETDATA>) { if ($get =~ /<date2>/) { $get =~ s/<date2>/$date2/g; } if ($get =~ /<year><month>/) { if ($date <= 7) { $month = &minus_month($month); $newdate = &validate_Month($month); $date = ($newdate + $date); $date = $date - 7; # $year = &validate_Year($month); $month = &change_mo2num($month); $get =~ s/<year>/$year/g; $get =~ s/<month>/$month/g; $get =~ s/<date>/$date/g; print WRITE "$get"; } else { #month stays as current month $newdate = &validate_Month($month); # $year = &validate_Year($month); $month = &change_mo2num($month); $date = $date - 7; if ($date < 10) { $date = "0" . $date; } $get =~ s/<year>/$year/g; $get =~ s/<month>/$month/g; $get =~ s/<date>/$date/g; print WRITE "$get"; } } else { print WRITE "$get"; } } close(WRITE); close(GETDATA); ###### SUBS START HERE ############### sub validate_Year { my ($month) = @_; if ($month eq "jan") { $yr = qx(date +%Y); return $yr; } else { ####### Must Change ####### $yr = "2003"; } } sub validate_Month { my ($m) = @_; if (($m eq "JAN") xor($m eq "MAR") xor ($m eq "MAY") xor ($m eq "JUL") xor ($m eq "AUG") xor ($m eq "OCT") xor ($m eq "DEC")) { $day = 31; return $day; } elsif (($m eq "APR") xor($m eq "JUN") xor ($m eq "SEP") xor ($m eq "NOV")) { $day = 30; return $day; } elsif ($m = "FEB") { if (($testyear eq "2003") xor ($testyear eq "2005") xor ($tes +tyear eq "2007") xor ($testyear eq "2009")) { $day = 28; return $day; } else { $day = 29; return $day; } } } sub add_month { my ($month) = @_; if ($month eq "JAN") { $month = "FEB"; return $month; } elsif ($month eq "FEB") { $month = "MAR"; return $month; } elsif ($month eq "MAR") { $month = "APR"; return $month; } elsif ($month eq "APR") { $month = "MAY"; return $month; } elsif ($month eq "MAY") { $month = "JUN"; return $month; } elsif ($month eq "JUN") { $month = "JUL"; return $month; } elsif ($month eq "JUL") { $month = "AUG"; return $month; } elsif ($month eq "AUG") { $month = "SEP"; return $month; } elsif ($month eq "SEP") { $month = "OCT"; return $month; } elsif ($month eq "OCT") { $month = "NOV"; return $month; } elsif ($month eq "NOV") { $month = "DEC"; return $month; } elsif ($month eq "DEC") { $month = "JAN"; return $month; } } sub minus_month { my ($month) = @_; if ($month eq "JAN") { $month = "DEC"; return $month; } elsif ($month eq "FEB") { $month = "JAN"; return $month; } elsif ($month eq "MAR") { $month = "FEB"; return $month; } elsif ($month eq "APR") { $month = "MAR"; return $month; } elsif ($month eq "MAY") { $month = "APR"; return $month; } elsif ($month eq "JUN") { $month = "MAY"; return $month; } elsif ($month eq "JUL") { $month = "JUN"; return $month; } elsif ($month eq "AUG") { $month = "JUL"; return $month; } elsif ($month eq "SEP") { $month = "AUG"; return $month; } elsif ($month eq "OCT") { $month = "SEP"; return $month; } elsif ($month eq "NOV") { $month = "OCT"; return $month; } elsif ($month eq "DEC") { $month = "NOV"; return $month; } } #The 2 subroutines for date conversion sub change_num2mo{ my ($month) = @_; if ($month =~ /01/) { $num_month = "jan"; return $num_month; } if ($month =~ /02/) { $num_month = "feb"; return $num_month; } if ($month =~ /03/) { $num_month = "mar"; return $num_month; } if ($month =~ /04/) { $num_month = "apr"; return $num_month; } if ($month =~ /05/) { $num_month = "may"; return $num_month; } if ($month =~ /06/) { $num_month = "jun"; return $num_month; } if ($month =~ /07/) { $num_month = "jul"; return $num_month; } if ($month =~ /08/) { $num_month = "aug"; return $num_month; } if ($month =~ /09/) { $num_month = "sep"; return $num_month; } if ($month =~ /10/) { $num_month = "oct"; return $num_month; } if ($month =~ /11/) { $num_month = "nov"; return $num_month; } if ($month =~ /12/) { $num_month = "dec"; return $num_month; } } sub change_mo2num { my ($month) = @_; if ($month =~ /JAN/) { $num_month = "01"; return $num_month; } if ($month =~ /FEB/) { $num_month = "02"; return $num_month; } if ($month =~ /MAR/) { $num_month = "03"; return $num_month; } if ($month =~ /APR/) { $num_month = "04"; return $num_month; } if ($month =~ /MAY/) { $num_month = "05"; return $num_month; } if ($month =~ /JUN/) { $num_month = "06"; return $num_month; } if ($month =~ /JUL/) { $num_month = "07"; return $num_month; } if ($month =~ /AUG/) { $num_month = "08"; return $num_month; } if ($month =~ /SEP/) { $num_month = "09"; return $num_month; } if ($month =~ /OCT/) { $num_month = "10"; return $num_month; } if ($month =~ /NOV/) { $num_month = "11"; return $num_month; } if ($month =~ /DEC/) { $num_month = "12"; return $num_month; } }

In reply to 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.