Joshua Colson wrote:
On Thu, 2006-06-08 at 00:55 +0200, Flemming Greve Skovengaard wrote:

If you are just going to print the day number and you have other dates in a
similar format why not just use:

print +(split /\s+/, $date)[2];

Well, in this particular instance, I am. However, there have been at
least a few times in the past that I've wanted to achieve the same thing
and it always ends up looking like comic book curse words. Also, I
prefer to validate the input. This example would match even if the third
column weren't digits (more specifically, digits in the range of 1 to
31).

Thanks.


OK, how about this.
This however does not check whether the month can have 31 days.

#!/usr/bin/perl

use strict;
use warnings;

my @dates = (
    "Wed Jun  7 14:27:38 2006",
    "Wed Jun 47 14:27:38 2006",
);

foreach my $date ( @dates ) {
    my $day_number = ( split /\s+/, $date )[2];
    if ( $day_number =~ m/
        ^(?:
            [12]?[1-9]  |   # 1-9, 11-19, 21-29 or
            [1-3]0      |   #  10,    20,    30 or
            31              #  31
        )$
        /x ) {

        print $day_number, "\n";
        # Or whatever you want to do with the day number
    }
}


--
Flemming Greve Skovengaard                   Just a few small tears between
a.k.a Greven, TuxPower                       Someone happy and one sad
<[EMAIL PROTECTED]>                  Just a thin line drawn between
4181.33 BogoMIPS                             Being a genius or insane


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to