This code lists each date since January 1, 1896.

The form that these dates will be inputted to requires the format YYYYMMDD, thus the first through ninth MM and DD values were coded as literal strings with "0" in front.

I can tell that this flow is screaming for the use of hashes, subs, or improved loop structures, so fill me in on some good ways to improve.

Thanks!

#!/usr/bin/perl ############################################## ####### ii.pl ######################### ############################################## # This program lists all dates from 01/01/1896 # to 12/31/2009. # The list will be used to submit each date on # the wx station site in order to gather # the required data. ############################################## # COMMENTS COME AFTER THE LINE OF INTEREST use strict; # to ensure scoping obedience use warnings; # provides warnings when syntax is off use diagnostics; # helps use Number::Ops qw(:all); # for using 'isint()' operator when determining leap years my (@nonleaps, @leaps, @dates); # arrays to hold non leap years, leap years, and then all dates my @yrs = (1896 .. 2009); # all years in the period of record (POR) my @mos = ('01', '02', '03', '04', '05', '06', '07', '08', '09', 10, 11, 12); # literal strings must be used for months 1 through 9 because # the web form requires input of 8 characters only (YYYYMMDD) my @feb = ('01', '02', '03', '04', '05', '06', '07', '08', '09', 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28); # days in the month of feb on non leap years. Literal strings # used for the first 9 days for the same reason as above. my @leap = ('01', '02', '03', '04', '05', '06', '07', '08', '09', 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29); # feb days during leap years my @thirty = ('01', '02', '03', '04', '05', '06', '07', '08', '09', 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30); # for those months with 30 days my @thirtyone = ('01', '02', '03', '04', '05', '06', '07', '08', '09', 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31); # for those with 31 days #BEGIN FLOW CONTROL# #################### foreach my $yrs (@yrs) {# for each year foreach my $mos (@mos) {# for each month in each year if (isint($yrs/4) && $yrs != 1900) {# if it's a leap year if ($mos=='02') {# if it's feb in a leap year foreach my $leap (@leap) {# then add each day in feb to the date below my $date = "$yrs$mos$leap";# date is YYYYMMDD push (@dates, "$date\n"); # add the date to the a +rray } } elsif ($mos=='04' || $mos=='06' || $mos=='09' || $mos==11) {# if it's april, june, sept, or nov foreach my $thirty (@thirty) {# add thirty days to each month my $date = "$yrs$mos$thirty";# date is YYYYMMDD push (@dates, "$date\n"); # add the date to th +e array } } else {# if its any of the other months foreach my $thirtyone (@thirtyone) {# add 31 days my $date = "$yrs$mos$thirtyone"; push (@dates, "$date\n"); } } } else {# if it's not a leap year..(do the same, but 28 days in feb). +.. if ($mos=='02') { foreach my $feb (@feb) { my $date = "$yrs$mos$feb"; push (@dates, "$date\n"); } } elsif ($mos=='04' || $mos=='06' || $mos=='09' || $mos==11) { foreach my $thirty (@thirty) { my $date = "$yrs$mos$thirty"; push (@dates, "$date\n"); } } else { foreach my $thirtyone (@thirtyone) { my $date = "$yrs$mos$thirtyone"; push (@dates, "$date\n"); } } } } } print @dates; open (FH, ">dates.txt");# open a new file for output print FH @dates;# print the array of all dates to the file

In reply to Improve my Code: Date List by cheech

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.