From: Dark Ambient Date: 2006-07-03T22:59:24+09:00 Subject: Re: if problem in loop causing weirdness I 'suspected' this as I had a similar issue (different behaviour) in my last previous thread. What I had to do to correct things is if 'yes', push into the new array AND still delete from former array, and if 'no', just delete from former/existing array. Weird I think(?) especially in regards to the 'no', but without the delete, it continues to loop on the gets. Reposting my code just to show what I've changed. Things are working correctly not (until at least I add the next function :)). Thank you for the help! destdir = Dir['C:/testfilesmoved/*.*'] newmove = [] destdir.each do |name| pics2bmoved.each do |x| if File.exist?(name) && File.compare(x, name) == true puts x + ' already exists' puts 'Do you want to overwrite? "yes" or "no"?' ow = gets.chomp.downcase if ow == 'yes' <======== corrected newmove.push(x) pics2bmoved.delete(x) else <============= corrected. pics2bmoved.delete(x) puts 'okay, file will not be overwritten' end # for if file.exist? end # ow end # end for pics2bmoved loop end # end for destdir loop puts if newmove.empty? puts 'There are no files to be moved' else puts 'The following files will be moved' puts newmove end # for pic2bmoved list -------------------------------------------------------------------------------------------------------------- On 7/3/06, Travis Michel wrote: > I suspect that the problem is with the deletion from an array within a loop > of that array. > > Solution: construct a different array such that => if ow != 'no' then > new_array.push(x); end; > > consider the following test. > > irb(main):013:0> x = [1,2,3,4] > => [1, 2, 3, 4] > irb(main):014:0> x.each { |ii| > irb(main):015:1* print "ii = #{ii}\n" > irb(main):016:1> print "x = #{x}\n" > irb(main):017:1> x.delete(ii) > irb(main):018:1> print "x now = #{x}\n" > irb(main):019:1> } > ii = 1 > x = 1234 > x now = 234 > ii = 3 > x = 234 > x now = 24 > => [2, 4] > > > While this does not demonstrate your issue persay, it does however > illustrate a bug. ii never equals the value of the original second index, > because the array's iterator does not go back a step on deletion. > > Hope this helps. > > -trav > -- > p [151,40,154,157,166,145,40,162,165,142,171].collect { |ii| eval > '"\\'+ii.to_s+'"' }.join > >