Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Newbie: need algorithm to evaluate no file exist

by Anonymous Monk
on Aug 18, 2010 at 14:37 UTC ( #855793=perlquestion: print w/replies, xml ) Need Help??

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

Hi, inside an ftp session i need to know if a file has arrive in the last hour, if not then send an email. the olders files on the remote server does'nt matter.

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"; } envia_mail (); } $ftp->quit or die $ftp->message;

this print me the new files. this is not what i want, beside this evaluate all the files. In short terms. if no file in the last hour send a mail. I'll appreciate if you point me to clear the idea

Replies are listed 'Best First'.
Re: Newbie: need algorithm to evaluate no file exist
by kennethk (Abbot) on Aug 18, 2010 at 15:07 UTC
    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;
      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

      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.
Re: Newbie: need algorithm to evaluate no file exist
by jethro (Monsignor) on Aug 18, 2010 at 15:17 UTC

    Use a boolean variable to remember whether you found one or more newer files. After the loop this boolean variable tells you what you want to know. Since it is not strictly a boolean variable it also tells you how many newer files it found

    ... my $newfilefound=0; $localtimenoformat=time; for $file (@files) { $mdtmnoformat=$ftp->mdtm($file); unless ( ($localtimenoformat - $mdtmnoformat) > 3600 ) { $newfilefound++; } } if (not $newfilefound) { envia_mail(); }
Re: Newbie: need algorithm to evaluate no file exist
by murugu (Curate) on Aug 18, 2010 at 15:16 UTC
    Hi,

    As kennethk pointed, use a flag inside the loop and check itoutside and send mail according to the flag value.

    Is there any specific reason to create @files array instead of using $ftp->ls directly in loop? Is it for @files reuse in the other parts of the code?

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2019-08-24 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?