Howdy Monks!

Here it is, the final version of the date conversion script...and now that it works it is truly "happiness".

I added some user interface niceties, eliminated two of the 5 regexes (I know more could go too), fixed the rest of the problems it was having, and turned it in to my instructor.

So now that it is done and handed in, this is where the real fun begins. How would you have done any of it differently? Or maybe all of it differently? UPDATE - we weren't allowed to use any modules - only our own code

BTW - I just wanted to say to you all that in my brief membership here at PM I have learned more Perl than I have in any class at any time, and for that I thank you all. Without your valuable assistance I might never have been able to write this script the way it turned out. Thanks!

ps - I actually _like_ Perl now :-)

update - to trace this script to its birth, go to this post

#!usr/bin/perl #Script to parse dates with the following formats: #Apr 8 1984, Apr 08 84, 4/8/84, 04/08/84, 08 Apr 1984 use warnings; use strict; #blah blah blah once print "\n\n\n\n"; print "Date Converter, v1.0\n"; print "this program assumes that a year from 10 to 99 is in the 1900's +\n"; print "and that a year from 00 to 09 is in the 2000's\n"; print "and will loop until you type 'q' to break out of it.\n"; #loop script forever, atbe while (1) { #declare my vars and make sure they're empty my ( $MM, $DD, $YY, $YYYY ); #take in date or q at command line and make text lowercase print "please enter a date\n"; chomp (my $date = <>); if ($date eq "q") { exit(); } $date = lc ($date); #run date entered thru series of regex filters, #and subsequently perform perly goodness on them. #send result to output subroutine if ($date =~ /[a-zA-Z]{3}\s\d{1,2}\s\d{2,4}/) #parse Apr 08 84 or Apr +8 1984 { my @dateparts = split (/\s/ , $date); $MM = $dateparts [0]; $DD = $dateparts [1]; $YY = $dateparts [2]; if (10 <= $YY && $YY <= 99) {$YYYY = $YY + 1900} elsif (0 <= $YY && $YY <= 9) {$YYYY = $YY + 2000} else {$YYYY = $YY} output ($MM, $DD, $YYYY); } elsif ($date =~ /\d{1,2}\/\d{1,2}\/\d{2}/) #parse 04/08/84 or 4/8/84 { my @dateparts = split (/\// , $date); $MM = $dateparts [0]; $DD = $dateparts [1]; $YY = $dateparts [2]; if (10 <= $YY && $YY <= 99) {$YYYY = $YY + 1900} elsif (0 <= $YY && $YY <= 9) {$YYYY = $YY + 2000} output ($MM, $DD, $YYYY); } elsif ($date =~ /\d{1,2}\s([a-zA-Z]{3})\s\d{4}/) #parse 08 Apr 1984 { my @dateparts = split (/\s/ , $date); $DD = $dateparts [0]; $MM = $dateparts [1]; $YYYY = $dateparts [2]; output ($MM, $DD, $YYYY); } else #contingency plan { print "\nyour entry is not in a format recognized by this script.\ +n"; print "please use only the following:\nApr 8 1984, Apr 08 84, 4/8/ +84, 04/08/84, 08 Apr 1984, or 'q' to quit.\n\n"; } #output subroutine #take in $MM $DD $YYYY, parse $MM with %months #and print "fullmonth day, year" to STDOUT sub output { my @outputdates = @_; my %months = ( jan => "January", feb => "February", mar => "March", apr => "April", may => "May", jun => "June", jul => "July", aug => "August", sep => "September", oct => "October", nov => "November", dec => "December", 1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December", '01' => "January", '02' => "February", '03' => "March", '04' => "April", '05' => "May", '06' => "June", '07' => "July", '08' => "August", '09' => "September" ); print "$months{$outputdates[0]} $outputdates[1], $outputdates[2]\n +\n"; } }

In reply to Final date conversion happiness script by ctp

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.