This works with the test data supplied. Changes for differently structured data would be trivial, but...
Wise ones, would any care to suggest amendments that would improve the syntax or algorithm (without making it so concise and idiomatic as to be unintelligible to rookies)? I recognize the clumsy creation of the hash and subsequent deletion of the first element as one prime candidate (but so far neither docs nor Cookbook have insinuated anything better in my brain). Also, as the conversion of mday and non-conversion of month and year (at lines 34-36) demonstrate, I just haven't "got" something in the litt re Time::Local.
update: graff's point tells us that I just plain confused $mday with $mon (which is used in the sub). And BigLug offers a concise answer re creation of the hash.
Both replies offer just what I was hoping for! But it might clarify for others if s/improvements/critiques/ in the title
and if I make it explicit that I do indeed know and honor the wisdom of "use the module; don't re-invent the wheel." But I need to be better at the basics ... and -- based on experience with other self-teaching efforts -- doing self-imposed puzzles like this and heeding the critiques really works well for me.
#!C:/Perl/bin -w use strict; use vars qw ( @old $old %data $sec $min $hour $mday $mon $year $etime +); use Time::Local qw (timelocal); # create hash to hold filename and $etime with a fake key # and value -- both to be deleted after the foreach on DATA %data = ( "file", 0 ); @old = <DATA>; foreach $old(@old) { if ( $old =~ / # 1 of many ways. For clarity, not elegance ^ # start at beginning of line [MTWFS] # Any one UpperCase first_letter_of_day_of_week ( +english) [a-z]{2} # Any lowercase letter, 2 times exactly \s # One whitespace char ([A-Za-z]{3}) # UC or lc in range A-Z or a-z, 3 times -- CAPTUR +E month $1 \s # One whitespace char ([\d]{1,2}) # one or two digits - day_of_month -- capture day +_of_month $2 \s # One whitespace char ([\d]{1,2}): # Any dec dgt or :, 1 OR 2 times -- CAPTURE Hour +$3 - fol by : ([\d]{2}): # Any dec dgt or :, 2 times -- CAPTURE Min $4- f +ol by : ([\d]{2})\s # Any dec dgt or :, 2 times -- CAPTURE Sec $5 - f +ol by sp [ECMP][SD]T # Eastern, Central...Std or Day...T \s # One whitespace (200\d) # One whitespace fol by "200" and 1 more dgt CAPT +URE Yr $6 .* /x # anything_except_\n to end_of_record, end m//, +extended syntax ) # end condition { # begin action block $sec = $5; $min = $4; $hour = $3; $mday = ( $2 - 1 ); # mday is in range 0-30 for timeloca +l $mon = $1; # needs to be in 0..11 form: &conver +t_month $year = $6; # ?! timelocal years are years - 190 +0 &cvrt_mon_lcl($mon); # etime from $old form to Epoch seconds $etime = timelocal($sec, $min, $hour, $mday, $mon, $year); chomp ($old); # clean \n off $old. $old =~ s/is : / /; # shorten to DateTime:depth # print "\$etime for \"$old\"|| \t$etime\n"; $data{$old}= $etime; # PUSH etime to the hash } else { print "no match \n"; } } # NOW, delete the (fake) elements with which we initialized the hash delete $data{"file"}; # NEXT convert current time to seconds since EPOCH my $now = time(); # ASSESS the DATA elements for > or < 45 days old while (($old, $etime) = each %data) { if ( ($now - $etime) > (24*60*60*45) ) { # 24hr*60min*60sec*45 + days print " \"$old\" - is MORE than 45 days old\n"; print "\t It's roughly ".int(($now-$etime)/86400)." days old\n\n +"; } else { # This errs in favor of "LESS THAN" (no == test abo +ve) print " \"$old\" - is LESS than 45 days old\n"; print "\t It's only about ".int(($now-$etime)/86400)." days old\ +n\n"; } } exit; ##### sub cvrt_mon_lcl -- FOR timelocal and localtime; gmtime is diff +erent! sub cvrt_mon_lcl { if ($mon eq "Dec") { $mon = 11; }elsif ($mon eq "Nov") { $mon = 10; }elsif ($mon eq "Oct") { $mon = 9; }elsif ($mon eq "Sep") { $mon = 8; }elsif ($mon eq "Aug") { $mon = 7; }elsif ($mon eq "Jul") { $mon = 6; }elsif ($mon eq "Jun") { $mon = 5; }elsif ($mon eq "May") { $mon = 4; }elsif ($mon eq "Apr") { $mon = 3; }elsif ($mon eq "Mar") { $mon = 2; }elsif ($mon eq "Feb") { $mon = 1;</readmore> }elsif ($mon eq "Jan") { $mon = 0; } return($mon); }
__DATA__ Fri Dec 3 23:07:21 EST 2004 : Depth is : 234 Sat Dec 4 00:07:29 EST 2004 : Depth is : 123 Sun Dec 5 03:07:32 EST 2004 : Depth is : 144 Tue Feb 8 05:21:18 EST 2005 : Depth is : 11 Sat Feb 26 14:13:12 EST 2005 : Depth is : 4567 Fri Mar 11 17:09:38 EST 2005 : Depth is : 265 Sat Mar 12 07:07:27 EST 2005 : Depth is : 1654
In reply to hash, Date:Calc use - seeking improvements by ww
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |