garyprmr has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Problem in date checking script
by ferreira (Chaplain) on Jan 18, 2007 at 12:35 UTC

    The problem with your piece of code is that the regex you tried to build include "/" and the delimiters are also "/". This is going to work:

    chomp($value = <STDIN>); if ($value =~ m#(0[1-9]|[12][0-9]|3[01])[-/.](0[1-9]|1[012])[- /.](\d\ +d\d\d)#) { print "date"; } else { print $value; print "not a date"; }

    Here I used alternative delimiters by using m## syntax. You may read more about it at section "Regexp Quote-Like Operators" in perlop.

    And while we're at the issue of validating dates, your code may work for dates like "01/02/2007" and recognize "32/04/2007" as bad, but it will accept things like "29/02/2007" and the like. If you want correctness and minimum effort, you can use some CPAN module to check it out for you. For instance, using DateTime::Format::Strptime, you may write code like this:

    se DateTime::Format::Strptime; my $dt_format = DateTime::Format::Strptime->new(pattern => "%d-%m-%Y") +; sub val_date { my $dt = shift; $dt =~ s|[./]|-|g; # '.' and '/' become '-' for simplicity return $dt_format->parse_datetime($dt); } my $s = '01-02-2007'; my $dt = val_date($s); if ($dt) { print "valid date\n"; } else { print "not a date\n"; }

    As an extra bonus, the return of val_date gives you a DateTime object which is much better to handle than just a string.

Re: Problem in date checking script
by shmem (Chancellor) on Jan 18, 2007 at 12:31 UTC
    See How (Not) To Ask A Question.

    What problem do you have with that? My problem is that I can't read your post - the code is all messed up.

    After composing a post and hitting [preview], a line is displayed below the rendered output of the composed text, reading:

    If something looked unlike you expected it to you might need to check out Writeup Formatting Tips.

    Do so, please.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Problem in date checking script
by kyle (Abbot) on Jan 18, 2007 at 12:41 UTC

    Looks like your code should read:

    chomp($value = <STDIN>); if($value =~ /(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.] \d\d +\d\d)/) { print "date"; }else{ print $value; print "not a date"; }

    I notice there's a literal space between [- /.] and \d\d\d\d, which may not be what you intended. That's if I've correctly deciphered your very poorly formatted post.

Re: Problem in date checking script
by Anonymous Monk on Jan 18, 2007 at 12:23 UTC
    Sure, here you go:
    #!/usr/bin/perl die "I'm a lazy git who wants help but can't be bothered to explain my + problem properly. Kick me!\n"