in reply to Array question

While removing entries from an array like that is inadvisable, your code will not work as expected for two reasons:

  1. You only increment $arrayCounter after a delete, when it needs to be incremented on every loop.
  2. After a delete, on the next main loop, you attempt to reuse the deleted array element.

The following should work as you expect:

foreach my $currentremotefilelist (@remoteFilelist) { my $arrayCounter = -1; foreach my $currentlocalfilelist (@return2) { $arrayCounter++; # Increment on every loop iteration next if !defined $currentlocalfilelist; # Skip previousl +y deleted entries #myfile.txt =~ myfile.txt_date my $currentremotefilelistsize = -s $currentremotefilelist; my $currentlocalfilelistsize = -s $logSite.$currentlocalfileli +st; $currentlocalfilelist = $logSite.$currentlocalfilelist; if (($currentremotefilelist =~ /^$currentlocalfilelist/i) && ( +$currentremotefilelistsize == $currentlocalfilelistsize)) { print "$currentremotefilelist and $currentlocalfilelist al +so $currentlocalfilelistsize and $currentlocalfilelistsize They are E +qual and will not be copied\n\n"; delete $return2[$arrayCounter]; } else { print "$currentremotefilelist and $currentlocalfilelis +t also $currentlocalfilelistsize and $currentlocalfilelistsize are no +t EQUAL!!\n"; } } }

Updated: moved counter to above the next

Replies are listed 'Best First'.
Re^2: Array question
by AnomalousMonk (Archbishop) on Nov 20, 2009 at 18:24 UTC
    As pointed out by Corion, undefining an array element and then continuing to iterate over it again and again is highly problematic, and probably a Bad Thing™.

    However, if this is really necessary, it is not necessary to maintain a separate array index, which can be highly confusing. The element in question is already an alias and can be undefined by an  undef $currentlocalfilelist; statement.

Re^2: Array question
by Karger78 (Beadle) on Nov 20, 2009 at 17:27 UTC
    Ok, maybe i should explaine a bit further as it appears that there are a few different way to accomplish this. I have two arrays. RemoteFiles and LocalFiles. So I would like to do a compare against them to see if the files are the same. If they are remove them from the Remotefiles array. Note the RemoteFiles have a time stamp added to the file name as I am checking to see if they have been copied over before. Does this help?