Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Date manipulation

by advait (Beadle)
on Jul 11, 2008 at 15:26 UTC ( [id://696985]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,
My records are in following tab separated format
usage1 Thu Feb 28 17:30:47 2008 usage2 Fri Feb 29 03:55:22 2008 usage3 Fri Feb 29 04:00:46 2008 usage4 Fri Feb 29 04:10:48 2008
I want to retrieve all the rows which have date higher than feb 28 2008
i dont have any permission on server to install a module of perl thank you

Replies are listed 'Best First'.
Re: Date manipulation
by Tanktalus (Canon) on Jul 11, 2008 at 16:33 UTC

    Yes, even you can use CPAN. You can install perl modules in your home directory, or into your build tree (along with your code). Either that, or you can't even install your code so the question is moot.

    Now that you CAN use modules, you may want Text::CSV_XS to handle tab-separated data (though it may be overkill at this point, I always like planning for as much flexibility as possible, and since I don't need to write the code but merely call it, all the better). But you almost definitely DO want Date::Parse for parsing those strings into unix-times that you can use for simple comparison.

Re: Date manipulation
by olus (Curate) on Jul 11, 2008 at 15:52 UTC

    This is a solution regexp based. You should complete the %months hash to include all the other months.

    use strict; use warnings; my @dates = <DATA>; my %months = ('Jan' => 1, 'Feb' => 2, 'Dec' => 12, ); foreach my $str_date (@dates) { $str_date =~ /^\w*\s*\w*\s*(\w*)\s*(\d*).*(\d{4})$/; my($month, $day, $year) = ($1, $2, $3); my $date = sprintf("%4d%02d%02d", $year, $months{$month}, $day); if($date gt "20080228") { print $str_date; } } __DATA__ usage1 Thu Feb 28 17:30:47 2008 usage2 Fri Feb 29 03:55:22 2008 usage3 Fri Feb 29 04:00:46 2008 usage4 Fri Feb 29 04:10:48 2008

    That outputs

    usage2 Fri Feb 29 03:55:22 2008 usage3 Fri Feb 29 04:00:46 2008 usage4 Fri Feb 29 04:10:48 2008
      If you've got it, you could use the groovy "named capture" feature of Perl 5.10 to change that regex to:
      $str_date =~ /^\w*\s*\w*\s*(?<month>\w*)\s*(?<day>\d*).*(?<year>\d +{4})$/;
      and then the usage to:
      my $date = sprintf("%4d%02d%02d", $+{year}, $months{$+{month}}, $+ +{day});

        Thank you for the suggestion.

Re: Date manipulation
by pc88mxer (Vicar) on Jul 11, 2008 at 17:13 UTC
    i dont have any permission on server to install a module of perl
    Are you prohibited by management from installing new modules, or is it that you don't have the access permissions to do so?

    If it's only the latter, you can always install modules into a local directory. See the documentation about the PREFIX option to perl Makefile.PL.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://696985]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found