in reply to Array question

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"; } };

Replies are listed 'Best First'.
Re^2: Array question
by Karger78 (Beadle) on Nov 20, 2009 at 16:08 UTC
    Thanks I will give that a try. You see the issue i was running into is that I have two array's with a list of just the file names. But they could be in a different order, or one array could have more then the other, so I had to find away that we take the first file name, compare against all the names in the 2nd array, then go back to the 2nd file in the 1st array, and compare against all the file names in the 2nd array. Does that make sense?

      I would try to get away from needing to rely on things like "order". Which is why I suggest you use a hash, and building a unique key to find the matches between the two lists.

      Again, finding the criteria that make a match between the two lists is something only you can know. But if you have found and formulated what makes two elements "identical" in your case, it makes finding the match much easier, because then you can use that "identity" as the key into a hash and check whether an element with that key exists or not.