Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

time::piece Error parsing time at /usr/lib64/perl5/Time/Piece.pm line 469

by darkwell (Initiate)
on Mar 06, 2018 at 06:14 UTC ( #1210389=perlquestion: print w/replies, xml ) Need Help??
darkwell has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm comparing an array of email addresses with another array of emails with appended dates. I want to remove the dates, reformat them, sort them most recent first.

The script worked but ended before it worked through the entire array. I thought it timed out for some reason and fed the script a shorter array.Again it worked before completing the array. Again I shortened it and then it failed to work at all displaying the error: Error parsing time at /usr/lib64/perl5/Time/Piece.pm line 469.

#!/usr/bin/perl -wt use strict; use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); use Time::Piece; print "Content-type: text/html\n\n"; my (@buyers,@csv,@dif,@end,@sort,@time,@zed,$demo,$first,$second); open (CSV,"<../../public_html/emails.txt"); @csv=<CSV>; close (CSV); open (BUYERS,"<../../public_html/buyers.txt"); @buyers=<BUYERS>; close (BUYERS); foreach $first(@csv) { chomp($first); push(@zed,$first); foreach $_(@buyers) { if($_=~/$first/) { $_=~/\d+\/\d+\/\d+/; my $date=Time::Piece->strptime($&, '%m/%d/%Y'); my $newdate=$date->strftime('%Y%m%d'); push(@time, $newdate); } } @time = sort {$b <=> $a} @time; foreach (@time) { push(@zed,$_ ); } undef(@time); }

I can't determine what is generating the error. Thanks in advance.

  • Comment on time::piece Error parsing time at /usr/lib64/perl5/Time/Piece.pm line 469
  • Download Code

Replies are listed 'Best First'.
Re: time::piece Error parsing time at /usr/lib64/perl5/Time/Piece.pm line 469
by poj (Monsignor) on Mar 06, 2018 at 08:21 UTC
    I can't determine what is generating the error.

    You could use eval to trap the errors

    #!/usr/bin/perl use strict; use warnings; use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); use Time::Piece; # email addresses my $dir = '../../public_html/'; open CSV,'<',$dir.'email.txt' or die "CSV $!"; my @csv = <CSV>; close CSV; chomp(@csv); # build one regex my $re = join '|', map { quotemeta } sort { length $b <=> length $a } @csv; $re = qr/$re/; # seach text file my %date = (); my @error=(); my $lineno = 0; open BUYERS,'<',$dir."buyers.txt" or die "BUYERS $!"; for (<BUYERS>){ ++$lineno; if (/($re)/){ my $addr = $1; if (/(\d+\/\d+\/\d+)/){ my $date; if ( eval{ $date = Time::Piece->strptime($1, '%m/%d/%Y') ; 1} ){ push @{$date{$addr}},$date->strftime('%Y%m%d'); } else { push @error,"Error parsing '$addr' date : '$1' on line $lineno +\n"; } } } } # sort dates my @output; for my $addr (@csv){ push @output,$addr,sort @{$date{$addr} || []}; } # output print "Content-type: text/html\n\n"; print "$_\n" for @error; print "$_\n" for @output;
    Updated
    poj
      eval{ $date = Time::Piece->strptime($1, '%m/%d/%Y') }; if ($@){

      Please don't, see Bug in eval in pre-5.14. At least, use

      if (eval { $date = Time::Piece->strptime($1, '%m/%d/%Y'); 1 }) { # ~~~~~
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        Thanks poj and choroba. Eval solved it though I wish I understood why. I first tried:

        if (eval { $date = Time::Piece->strptime($1, '%m/%d/%Y'); 1 }) {

        as poj suggested but this generated the repetitive date 19700101. I tweaked it with:

        if (eval { $date = Time::Piece->strptime($&, '%m/%d/%Y'); 1 }) {

        and it generated what I had hoped.
        I read the docs on eval but I'm still unclear on what problem it resolved.
        Thanks again

Re: time::piece Error parsing time at /usr/lib64/perl5/Time/Piece.pm line 469
by Anonymous Monk on Mar 06, 2018 at 06:25 UTC
    "Error parsing time" and you dont show us the time it cant parse?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1210389]
Approved by marto
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2018-06-20 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (117 votes). Check out past polls.

    Notices?