Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Finding the intersection of two arrays is described in perlfaq 4. The short version: use a hash. So incorporating fruitures notes from above, here is my (untested, since I do not have access to a suitable ftp server) try:
#!/usr/bin/perl -w use strict; use Net::FTP; my $host = 'www.ftp.com'; my $user = 'anonymous'; my $pass = 'updatepuller@ftp.com'; my $remote_dir = '/pub/antivirus/NAV/signatures'; my $destination_dir = 'current-sig';
Here I read the contents of lastupdate.txt into the array @lupd. If the file does not exist, @lupd will be left empty (no need for a special flag).
my @lupd = (); if(-e "./lastupdate.txt") { open(LASTUPDATE,"./lastupdate.txt") or die "Error while opening lastupdate.txt ($!)"; chomp( @lupd = <LASTUPDATE> ); close(LASTUPDATE); }
The following construct is called a hash slice. It is described in more detail in this page
my %haveit; @haveit{ @lupd } = (1) x @lupd; my $ftp = Net::FTP->new($host, Debug => 1); $ftp->login($user,$pass); $ftp->type("I"); my @listing = grep /x86\.exe$/i, $ftp->ls($remote_dir); my @newupdate; foreach my $file (@listing) {
The hash slice above produced a hash which maps every known filename to a true value. So if the file has already been downloaded, we can simply skip the download. When the download is unsuccessful, the file is skipped as well.
next if $haveit{ $file }; $ftp->get($file) or next; push @newupdate, $file; } $ftp->quit; open(NEWUPDATE,">>./lastupdate.txt") or die "Couldn't open lastupdate.txt for appending ($!)"; print NEWUPDATE map "$_\n", @newupdate; close(NEWUPDATE) or die "Closing lastupdate.txt failed ($!)";
You might also want to think about locking if there is any chance that two instances of this program run at the same time.

In reply to Re: Anti-Virus Signature Updates by rincew
in thread Anti-Virus Signature Updates by linebacker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (3)
    As of 2019-12-08 04:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Strict and warnings: which comes first?



      Results (162 votes). Check out past polls.

      Notices?