Finding Exact Matches in Text Strings

   



Verifying Date Formats in Perl

The Perl script in Listing C.23 uses an array to store the number of days in each month (including leap years) and the Perl split function in order to validate a date string.

Listing C.23 verifyDate1.pl.

start example
my($date)  = "11/25/2003"; my($month) = 0; my($day)   = 0; my($year)  = 0; my(@daysInMonth) = (0,31,28,31,30,31,30,31,31,30,31,30,31); ($month, $day, $year) = split(/\//, $date); if( $year % 4 == 0 ) {    if( ($year % 100 == 0) && ($year % 400 != 0) )    {       $daysInMonth[1] = 29;    } } if( $year >= 1 ) {    if( ($day <= $daysInMonth[$month]) && ($day >= 1) )    {       if( ($month > 0) && ($month <= 12) )        {          print "Date $date is valid\n";       }       else       {          print "Month $month is out of range\n";       }    }    else    {       print "Day $day is out of range\n";    } } else {    print "Year $year must be at least 1\n"; }
end example

Remarks

You can invoke the Perl script verifyDate1.pl from the command line as follows,

perl -w verifyDate1.pl

and the output will be as follows:

Date 11/25/2003 is valid



   



Fundamentals of SVG Programming. Concepts to Source Code
Fundamentals of SVG Programming: Concepts to Source Code (Graphics Series)
ISBN: 1584502983
EAN: 2147483647
Year: 2003
Pages: 362

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net