#!usr/bin/perl #Script to parse dates such as the following: #Apr 8 1984, Apr 08 84, 4/8/84, 04/08/84, 08 Apr 1984 use warnings; use strict; #declare my vars and make sure they're empty my $MM = $DD = $YY = $YYYY = (); #take in date at command line and make text lowercase chomp (my $date = <>); $date = lc ($date); #parse Apr 8 1984 if ($date =~ /^\[a-zA-Z]{3}\s\d\s\d{4}$/){ split $date and assign each part to $MM $DD and $YYYY; output ($MM, $DD, $YYYY); #parse Apr 08 84 } elsif ($date =~ /^\[a-zA-Z]{3}\s\d{2}\s\d{2}$/) { split $date and assign each part to $MM $DD and $YY; convert $YY to 4 digits and assign to $YYYY; output ($MM, $DD, $YYYY); #parse 4/8/84 } elsif ($date =~ /^\d\/\d\/\d{2}$/) { split $date and assign each part to $MM $DD and $YY; convert $YY to 4 digits and assign to $YYYY; output ($MM, $DD, $YYYY); #parse 04/08/84 } elsif ($date =~ /^\d{2}\/\d{2}\/\d{2}$/) { split $date and assign each part to $MM $DD and $YY; convert $YY to 4 digits and assign to $YYYY; output ($MM, $DD, $YYYY); #parse 08 Apr 1984 } elsif ($date =~ /^\d{2}\s\[a-zA-Z]{3}\s\d{4}$/) { split $date and assign each part to $MM $DD and $YYYY; output ($MM, $DD, $YYYY); #contingency plan } else { print "your date is not of a recognizable format...good day.\n"; } sub output { #take in $MM $DD $YYYY with @_, parse $MM with %months #and print "fullmonth day, year" to STDOUT 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 "$MM's{value} $DD's{value}, $YYYY's{value}\n"; }
In reply to More Date Conversion Happiness, Part 2 by ctp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |