Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-03-19 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found