You're modifying the array you're iterating over. Don't do that.

Personally, whenever doing a nested iteration over two arrays, I try to convert at least one the inner array into a hash and do a hash lookup into the inner array. This is usually faster and makes for simpler and shorter code too.

Of course, the challenge is to find a good common key to use in the hash. In your case, it seems that you only want to act upon a file in @return2 if it has the same size as a file in @remoteFilelist, and if its name starts with the same letters as that file. Your comment indicates that all local files are named myfile.txt_date. So the key could be myfile.txt|localsize:

# First, build the hash of names+size of all the local files my %local_file; for my $filename (@return2) { $filename =~ /^(.*)_(20\d{6})$/ or die "Couldn't find date part in filename '$filename'"; my $remote_name = $1; my $local_size = -s $filename; my $key = join "|", $remote_name, $local_size; $local_file{ $key } = $filename; }; for my $remote_file (@remoteFilelist) { my $remote_size = -s $remote_file; my $key = join "|", $remote_name, $remote_size; if (! exists $local_file{ $key }) { print "$remote_file does not exist locally, or exists locally +but with the wrong size.\n"; } else { print "$remote_file exists locally as $local_file{ $key }\n"; } };

In reply to Re: Array question by Corion
in thread Array question by Karger78

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, details, 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, summary, 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.