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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |