To compare dates in a script

Discussion in 'Perl Misc' started by clearguy02@yahoo.com, Feb 7, 2005.

  1. Guest

    Hi all,

    Below is the scenario.

    I have a file that has two entrees (a name and a date) seperated by a
    tab. I need to get only all those names in the file if the date on the
    each line is greater than a certain date, 12-31-2004.

    Script
    #######################
    $current = "31-Dec-04";
    foreach (<DATA>) {
    ($f, $l) = /(\w+)\t+(\w+)/;
    print $f if ($l > $current);
    }
    __DATA__
    5.5.25.50 01-Feb-05
    TEST1 22-Jul-04
    BSTOP 03-Sep-02
    DB00004639 21-Jan-05
    DB00004693 25-Jan-05
    CDM_3.1.0 27-May-03
    ############################

    I think I am failing on comparing stuff. How can I rectify the above
    code?

    Thanks,
    Rider.
    , Feb 7, 2005
    #1
    1. Advertising

  2. writes:
    > I have a file that has two entrees (a name and a date) seperated by a
    > tab. I need to get only all those names in the file if the date on the
    > each line is greater than a certain date, 12-31-2004.


    You should probably get in the habit of searching CPAN before posting
    here-- that's not an insult, merely an observation that a lot of the
    time, you'll find the answer there first, and save time.

    In this case, you'd have found Date::Manip and Date::Calc, both of
    which will do what you want, more or less.

    -=Eric
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Schwartz, Feb 7, 2005
    #2
    1. Advertising

  3. wrote in news:1107813544.944788.294170
    @c13g2000cwb.googlegroups.com:

    > I have a file that has two entrees (a name and a date) seperated by a


    Just FYI: ITYM "entries".

    > tab. I need to get only all those names in the file if the date on the
    > each line is greater than a certain date, 12-31-2004.
    >
    > Script
    > #######################


    There is usually no need for this kind of separator. If you put a
    shebang line in your scripts, that serves nicely to set the script apart
    from the rest of the post, especially if you end your scripts with
    __END__.

    use strict;
    use warnings;

    missing. Please read the posting guidelines for this group.

    > $current = "31-Dec-04";


    my $current = "20041231";

    for reasons that will become clear later.

    > foreach (<DATA>) {


    This first reads the whole file. Better to use

    while (<DATA>) {

    > ($f, $l) = /(\w+)\t+(\w+)/;
    > print $f if ($l > $current);


    So, you have two strings and you compare them using the numeric operator
    '>'.

    Indeed, if you had turned on warnings, perl would have told you:

    Argument "31-Dec-04" isn't numeric in numeric gt (>) at a.pl line 9,
    <DATA> line 7.

    > I think I am failing on comparing stuff. How can I rectify the above
    > code?


    You could use a module for comparing dates. See CPAN if you want to go
    that route. On the other hand, you could also just convert your dates to
    the YYYYMMDD format and use string comparison:

    #! /usr/bin/perl

    use strict;
    use warnings;

    my $current = '20041231';
    my %months = (
    Jan => '01', Feb => '02', Mar => '03', Apr => '04',
    May => '05', Jun => '06', Jul => '07', Aug => '08',
    Sep => '09', Oct => '10', Nov => '11', Dec => '12',
    );

    while(<DATA>) {
    chomp;
    last unless $_;
    my ($file, $date) = split /\s+/;
    my ($day, $month, $year) = split '-', $date;
    # You'll need to figure out how far back the dates go
    # and adjust the if statement below accordingly
    if(0 + $year <= 50) {
    $year += 2000;
    } else {
    $year += 1900;
    }
    $month = $months{$month};
    print "$file\n" if $current le "$year$month$day";
    }

    __DATA__
    5.5.25.50 01-Feb-05
    TEST1 22-Jul-04
    BSTOP 03-Sep-02
    DB00004639 21-Jan-05
    DB00004693 25-Jan-05
    CDM_3.1.0 27-May-03

    __END__
    A. Sinan Unur, Feb 7, 2005
    #3
  4. wrote:
    > I have a file that has two entrees (a name and a date) seperated by a
    > tab. I need to get only all those names in the file if the date on the
    > each line is greater than a certain date, 12-31-2004.
    >
    > Script
    > #######################
    > $current = "31-Dec-04";
    > foreach (<DATA>) {
    > ($f, $l) = /(\w+)\t+(\w+)/;
    > print $f if ($l > $current);
    > }
    > __DATA__
    > 5.5.25.50 01-Feb-05
    > TEST1 22-Jul-04
    > BSTOP 03-Sep-02
    > DB00004639 21-Jan-05
    > DB00004693 25-Jan-05
    > CDM_3.1.0 27-May-03
    > ############################
    >
    > I think I am failing on comparing stuff.


    Obviously.
    warning: Argument "31Dec04" isn't numeric in numeric gt

    You need to make things comparable in order to compare them...

    > How can I rectify the above code?


    You can:

    1) enable strictures
    2) enable warnings
    3) declare the variables you are using
    4) use an appropriate module - I would suggest Date::parse
    5) refrain from unnecessarily loading the whole file into memory
    6) study "perldoc perlre" to find out that \w does not match
    what you think it matches

    use strict;
    use warnings;
    use Date::parse;
    my $current = str2time('31-Dec-04');
    while (<DATA>) {
    my ($f, $l) = /(.+)\t(.+)/;
    print "$f\n" if str2time($l) > $current;
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Feb 7, 2005
    #4
  5. mothra Guest

    <> wrote in message
    news:...
    > Hi all,
    >
    > Below is the scenario.
    >
    > I have a file that has two entrees (a name and a date) seperated by a
    > tab. I need to get only all those names in the file if the date on the
    > each line is greater than a certain date, 12-31-2004.
    >

    Something like this might work. It uses DateTime

    use strict;
    use warnings;
    use DateTime;
    use DateTime::Format::Strptime;

    #target date
    my $dt1 = DateTime->new(
    year => 2004,
    month => 12,
    day => 31,
    );

    my $Strp = new DateTime::Format::Strptime(
    pattern => '%d-%b-%y',
    );
    while (<DATA>) {
    chomp;
    my ( $file, $date ) = split /\s+/;
    my $dt2 = $Strp->parse_datetime($date);

    if ( $dt1 > $dt2 ) { print "Date from file is before\n"; }
    if ( $dt1 == $dt2 ) { print "Dates are the same\n"; }
    if ( $dt1 < $dt2 ) { print "Date from file is after\n"; }

    }

    __DATA__
    5.5.25.50 01-Feb-05
    TEST1 22-Jul-04
    BSTOP 03-Sep-02
    DB00004639 21-Jan-05
    DB00004693 25-Jan-05
    CDM_3.1.0 27-May-03

    I hope this helps

    Mothra
    mothra, Feb 8, 2005
    #5
  6. phaylon Guest

    clearguy02 wrote:

    > I have a file that has two entrees (a name and a date) seperated by a tab.
    > I need to get only all those names in the file if the date on the each
    > line is greater than a certain date, 12-31-2004.


    Just for curiousity: Only that date? I just ask because Dec 31 is the last
    day in '04, so you could just check the last two chars and put all out
    which are higher than 04, like this:

    #!/usr/bin/perl
    use warnings;
    use strict;

    while( <DATA> ) {
    chomp;
    if( /^.+\s+[0-9]{2}-[a-z]+-([0-9]{2})$/i ) {
    print "$_\n" if $1 > 4;
    }
    }

    __DATA__
    5.5.25.50 01-Feb-05
    TEST1 22-Jul-04
    BSTOP 03-Sep-02
    DB00004639 21-Jan-05
    DB00004693 25-Jan-05
    CDM_3.1.0 27-May-03
    __END__

    hth,
    phay

    --
    http://www.dunkelheit.at/
    Thru the darkness of futures past, the magician longs to see.
    One chants out between two worlds: Fire, walk with me.
    -- Twin Peaks, »Bob«
    phaylon, Feb 8, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    845
    Rob Schieber
    Sep 30, 2005
  2. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    180
    Mark Schupp
    Aug 9, 2004
  3. Replies:
    1
    Views:
    196
    Jano Svitok
    Jul 17, 2007
  4. kellygreer1

    RFC-822 dates into Ruby dates

    kellygreer1, Jun 8, 2008, in forum: Ruby
    Replies:
    1
    Views:
    178
    Eric I.
    Jun 8, 2008
  5. Dr J R Stockton

    FAQ Dates; Opera Dates.

    Dr J R Stockton, Jun 18, 2007, in forum: Javascript
    Replies:
    0
    Views:
    148
    Dr J R Stockton
    Jun 18, 2007
Loading...

Share This Page