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

Hi all, I studied perl 10 yrs ago and i didnot use now i am lost. If i have a file name db flat file mylist.log
2011/04/12 12:50:24|red|florida 2011/04/12 15:20:21|green|tampa 2011/04/12 15:30:12|red|miami
How do i compare if current time - fielone time > 216000 and field 2 is red then email me f1, f3 ?
#!/usr/dist/share/perl,v5.003/5bin.sun4/perl use Time::Local; $today = timelocal(localtime); $mailprog = "|/usr/sbin/sendmail -t"; $lst_email = 'someone@outthere.com'; open (DATA,"/mylist.log") || die ("Can't Open data File: $!\n"); @data=<DATA>; foreach $line (@data) { ($datestamp, $status, $location)=split(/\|/,$line); my $lastaction = &date_to_unix($datestamp); if (($today > ($lastaction - 216000)) && ($db =~ 'red') ) { MAILIT close DATA; sub date_to_unix() { my ($year,$mon,$day,$hour,$min,$sec) = $_[0] =~ /(\d+)\/(\d+)\/(\d+)\s(\d+):(\d+):(\d+)/; return undef unless ($day and $mon and $year); return timelocal(0,$min,$hour,$day,$mon-1,$year-1900); } open (MAILIT, "$mailprog") || &cgierr("Can't start mail program"); print MAILIT "To: $lst_email\n"; print MAILIT "Subject: $location is stuck \n\n"; print MAILIT "$location light has been red for more than an hour \ +n\n"; print MAILIT "please check it. \n\n"; print MAILIT "Here are the info\n"; print MAILIT "$datestamp and $location"; close (MAILIT);

Replies are listed 'Best First'.
Re: Compare field andsend mail
by Cristoforo (Curate) on Apr 13, 2011 at 01:50 UTC
    I would suggest calculating the time to be compared against once at the beginning of the script, and directly compare that with the date/time you extract from the file you are reading. The same type problem was discussed here.

    One advantage you have in your file is that the format for the date/time sorts naturally because the most significant parts of the date begin at the left going to the right.

    That is 'YYYY/MM/DD HH:MM:SS' is in a good order for sorts or comparisons.

    #!/usr/dist/share/perl,v5.003/5bin.sun4/perl use strict; use warnings; use Time::Local qw/ timelocal_nocheck /;; use POSIX qw/ strftime /; my $secs = 3600; # 1 hour my ($s, $m, $h, $d, $mon, $y) = localtime; my $time_ago = strftime "%Y/%m/%d %H:%M:%S", localtime timelocal_nocheck $s - $secs, $m, $h, $d, $mon, $y; print "$time_ago was one hour ago\n"; my $mailprog = "|/usr/sbin/sendmail -t"; my $lst_email = 'someone@outthere.com'; # DATA should be saved as the spaecial filehandle # for self-contained programs (like this one) + #open (my $in, "/mylist.log") || die ("Can't Open data File: $!\n"); # while (<$in>) { while (<DATA>) { chomp; my ($datestamp, $status, $location) = split /\|/; if ($datestamp lt $time_ago && $status eq 'red') { # do email notification print "red for more than an hour\n$_\n"; } } __DATA__ 2011/04/12 12:50:24|red|florida 2011/04/12 15:20:21|green|tampa 2011/04/12 15:30:12|red|miami
    Update: I hour ago could have been written:

    my $hrs = 1;

    and then subtracted from $h in the $time_ago formation.

    my $time_ago = strftime "%Y/%m/%d %H:%M:%S", localtime timelocal_nocheck $s, $m, $h - $hrs, $d, $mon, $y;
    To avoid possible errors in the time calculation, you could use a module made for dealing with dates, like DateTime.

    use DateTime; my $date = DateTime->now->subtract(hours => 1); my $hr_before = $date->set_time_zone('local')->strftime("%Y/%m/%d %H:% +M:%S");
      Thanks for teaching me this, it remind me now. BTW, how do we do this in perl
      $today = timelocal(localtime); my $lastaction = &date_to_unix($datestamp); sub date_to_unix() { my ($year,$mon,$day,$hour,$min,$sec) = $_[0] =~ /(\d+)\/(\d+)\/(\d+)\s(\d+):(\d+):(\d+)/; return undef unless ($day and $mon and $year); return timelocal(0,$min,$hour,$day,$mon-1,$year-1900); } $duration = ($today - $lastaction)/3600;
      How do i get $duration in hours only like 6 hrs instead of 6.6565656657 (6.6 is okay, i can live with 6) I know we have something to ignore remainder but i cannot find it in google :-) Thanks again. == It work now, i used this code
      $durationTemp = ($today - $lastaction) / 3600; $duration=sprintf("%d",$durationTemp);
      Thanks