Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Formatting STDIN for date format

by kcott (Canon)
on Sep 19, 2013 at 07:19 UTC ( #1054788=note: print w/replies, xml ) Need Help??

in reply to Formatting STDIN for date format

G'day Ashley,

Welcome to the monastery.

You were sort of on the right track with capturing the elements of the date with "(\d{n})" constructs, but you appear to have thought that including variables within the regex would somehow assign to them. For starters, I'd recommend you look at "perlintro -- a brief introduction and overview of Perl" and "perlretut - Perl regular expressions tutorial".

If all you want to do is check the format and, if OK, build $sql, you can code this:

#!/usr/bin/env perl -l use strict; use warnings; while (<STDIN>) { chomp; if (/^(\d{2})-(\d{2})-(\d{4})$/) { print "... day='$1' month='$2' year='$3' ..."; } else { print 'Not DD-MM-YYYY format'; } }

Here's a sample run:

$ 19-9-2013 Not DD-MM-YYYY format 19/9/2013 Not DD-MM-YYYY format 30-02-2013 ... day='30' month='02' year='2013' ... 99-99-9999 ... day='99' month='99' year='9999' ... 19-09-2013 ... day='19' month='09' year='2013' ... 19-09-20133 Not DD-MM-YYYY format

That's correctly weeding out "19-9-2013" and "19/9/2013"; unfortunately, it's quite happy with possible typos like "30-02-2013" and completely bogus dates like "99-99-9999". So, you'll probably need to assign the captured values to variables and then validate them. There's lots of ways of doing this, and it may well depend on what sort of feedback you want to present to your users; here's a fairly straightforward method, using the builtin module Time::Piece, that differentiates between bad format, possible typo or leap year error, and completely nonsense dates:

#!/usr/bin/env perl -l use strict; use warnings; use Time::Piece; while (<STDIN>) { chomp; if (/^(\d{2})-(\d{2})-(\d{4})$/) { my ($day, $month, $year) = ($1, $2, $3); my $tp = eval { Time::Piece->strptime("$day$month$year", '%d%m +%Y') }; if (defined $tp) { if ($tp->ymd eq "$year-$month-$day") { print "... day='$day' month='$month' year='$year' ..." +; } else { print "INVALID DATE: $day-$month-$year"; } } else { print "BOGUS DATE: $day-$month-$year"; } } else { print 'Not DD-MM-YYYY format'; } }

Sample run:

$ 19-9-2013 Not DD-MM-YYYY format 19/09/2013 Not DD-MM-YYYY format 19-09-2013 ... day='19' month='09' year='2013' ... 99-99-9999 BOGUS DATE: 99-99-9999 32-02-2013 BOGUS DATE: 32-02-2013 31-02-2013 INVALID DATE: 31-02-2013 29-02-2013 INVALID DATE: 29-02-2013 29-02-2012 ... day='29' month='02' year='2012' ... 19-09-20133 Not DD-MM-YYYY format

-- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1054788]
[Corion]: Heh. Perl heredocs are nasty - Filter::Simple doesn't know whether the line continued after the heredoc or not. The heredoc payload starts on the next line.
[Corion]: So currently, Filter::Simple mangles your code/line numbers if you use heredocs ;)
[Corion]: Of course, from a certain angle, it doesn't matter if your code line continues after <<FOO, but it would be nice if Filter::Simple / Text::Balanced didn't mangle that...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-01-22 10:24 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (187 votes). Check out past polls.