ctp has asked for the wisdom of the Perl Monks concerning the following question:
#!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"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: More Date Conversion Happiness, Part 2
by Zaxo (Archbishop) on Jan 11, 2004 at 05:00 UTC | |
|
Re: More Date Conversion Happiness, Part 2
by davido (Cardinal) on Jan 11, 2004 at 05:35 UTC | |
|
Re: More Date Conversion Happiness, Part 2
by neuroball (Pilgrim) on Jan 11, 2004 at 03:18 UTC | |
by ctp (Beadle) on Jan 11, 2004 at 04:47 UTC | |
|
Re: More Date Conversion Happiness, Part 2
by TomDLux (Vicar) on Jan 11, 2004 at 16:14 UTC | |
by ctp (Beadle) on Jan 11, 2004 at 18:21 UTC | |
|
Re: More Date Conversion Happiness, Part 2
by CountZero (Bishop) on Jan 11, 2004 at 09:31 UTC | |
by ctp (Beadle) on Jan 11, 2004 at 18:28 UTC | |
|
Re: More Date Conversion Happiness, Part 2
by Not_a_Number (Prior) on Jan 11, 2004 at 19:37 UTC |