in reply to Newbie: need algorithm to evaluate no file exist

As I understand your question, you want to send an e-mail if the file times are all larger than 1 hour. If this is incorrect, I apologize and please respond with clarification.

One solution would be to set a flag if a new file is found and test that flag after the loop, like:

my $new_file = 0; for $file ($ftp->ls){ push @files, $file; } for $file (@files) { $localtimenoformat=time; $mdtmnoformat=$ftp->mdtm($file); unless ( ($localtimenoformat - $mdtmnoformat) > 3600 ) { print "$file"; print "Localtime No Format = $localtimenoformat"; print "MDTM No Format = $mdtmnoformat\n"; $new_file = 1; } } envia_mail() unless $new_file; $ftp->quit or die $ftp->message;

Replies are listed 'Best First'.
Re^2: Newbie: need algorithm to evaluate no file exist
by ikegami (Patriarch) on Aug 18, 2010 at 15:29 UTC
    Two small tweaks (for the OP, really).
    • for (...) { push @a, $_; }

      is a long way of doing

      push @a, ...;

      Depending on the context, it might make more sense to do just

      my @a = ...;
    • unless ( ($localtimenoformat - $mdtmnoformat) > 3600 ) {

      can more clearly be written as

      if ( ($localtimenoformat - $mdtmnoformat) <= 3600 ) {
    • $localtimenoformat=time; doesn't need to be executed multiple times.

    • What you call "no format" time could really be called "epoch" time.

    push @files, $ftp->ls; my $new_file = 0; my $current_time_epoch = time; for my $file (@files) { my $modified_time_epoch = $ftp->mdtm($file); if ( ($current_time_epoch - $modified_time_epoch) <= 3600 ) { print "$file\n"; print "Current time = $current_time_epoch\n"; print "MDTM = $modified_time_epoch\n"; $new_file = 1; } } envia_mail() if !$new_file; $ftp->quit or die $ftp->message;

    If those print statements are just for debugging purposes, you can exit the loop early.

    push @files, $ftp->ls; my $new_file = 0; my $current_time_epoch = time; for my $file (@files) { if ( ($current_time_epoch - $ftp->mdtm($file) ) <= 3600 ) { $new_file = 1; last; } } envia_mail() if !$new_file; $ftp->quit or die $ftp->message;

      Thanks Ikegami, you're right on all your suggestions. as a newbie i'm very confuse,but i'm RTFM. Thanks again

        You don’t need to apologize.   :-)

        “This bald-spot over here?   Why, that happened when I ripped my hair out trying to solve this problem!   And that bald-spot over there...”

        P.S.   Even if it’s not actually true, it sure beats the hell out of admitting that you’re just getting old!   :-D

Re^2: Newbie: need algorithm to evaluate no file exist
by Anonymous Monk on Aug 18, 2010 at 15:34 UTC

    Sorry kennethk for not being clear enough. i only want send an email when there is not new file (newer than 1 hour), everything else doesn't matter.

      You were plenty clear; I'm just very conscientious when I think I might be misunderstanding a question. My posted code should meet your spec. I left your code intact so it would be easier for you to see my changes and thus understand the algorithm. ikegami has some lovely suggestions for how simplify/improve your code.