Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Formatting STDIN for date format

by kcott (Chancellor)
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:

$ pm_parse_stdin.pl 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:

$ pm_parse_stdin_2.pl 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1054788]
help
Chatterbox?
[Corion]: I still somewhat wonder if it were worthwhile to replace init / systemd by make with a simple? Makefile listing the dependencies ...
[Discipulus]: i have a different opinion: automation is always worth: first i can use Perl (and this is good), then later you can reuse parts to automate others tasks. My $boss everytime say:'how much time you spend doing this?' So generally i present a perl solution
[Corion]: Discipulus: Yes, but the chart gives some limits on whether it's really worth spending time for saving your time. If you gain enjoyment, automating is still great, but it doesn't save time ;)
[Discipulus]: Corion are you would able to realize such thing? O_O
[Corion]: In the same vein I have a script that automates Firefox to enter some data into another system. It's not faster than the people using the script if they were to do it manually, but they prefer not having to check the data and not having typos when ...
[Corion]: ... entering the data
[Corion]: Discipulus: I don't know whether I could really do that, but the init process itself mostly launches other processes, and the whole startup is just following a path of dependencies and making sure they are all running. Which basically is what ...
[Discipulus]: when at work my time is (temporarly) owned by the firm, so i do not care (coworkers whatch movies.. I code Perl)
[Corion]: ... make already does, except for files instead of programs. But maybe with some /proc hackery, that could be eliminated and one could use plain make :-D
[choroba]: systemd just makes is asynchronous

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2017-07-27 09:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (408 votes). Check out past polls.