ctp has asked for the wisdom of the Perl Monks concerning the following question:
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"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Final date conversion happiness script
by Zaxo (Archbishop) on Jan 13, 2004 at 00:41 UTC | |
|
Re: Final date conversion happiness script
by ysth (Canon) on Jan 12, 2004 at 23:51 UTC | |
by ctp (Beadle) on Jan 13, 2004 at 00:21 UTC | |
|
Re: Final date conversion happiness script
by Roger (Parson) on Jan 13, 2004 at 00:59 UTC | |
|
Re: Final date conversion happiness script
by Roger (Parson) on Jan 13, 2004 at 00:17 UTC | |
by ctp (Beadle) on Jan 13, 2004 at 00:26 UTC |