From: Dark Ambient Date: 2006-07-04T01:52:47+09:00 Subject: Re: if problem in loop causing weirdness Condensed version: require 'ftools' Dir.chdir 'C:/testfilesmoved' require 'ftools' pic_names = Dir['C:/testfilesorig/*.*'] destdir = Dir['C:/testfilesmoved/*.*'] pics2bmoved = [] pic_names.each do |name| puts 'Do you wish to move ' + name + 'file?,"yes" or "no" ?' decision = gets.chomp.downcase if decision == 'yes' destdir.each do |x| if File.compare(name, x) == false pics2bmoved.push(name) # problem here, first with loop same as b4, so # i removed the files from destdir, so File.compare wouldn't hit true # yet, the pics2bmoved.push doesn't seem to work. else puts 'The file already exists in destination, overwrite? "yes" or "no" ' ow = gets.chomp.downcase if ow == 'yes' pics2bmoved.push(name) else puts 'This ' + name + ' will not be moved' end # for ow==yes end # for File.compare end # for destdir loop end # for decision == yes end # for pic_names loop if pics2bmoved.empty? puts puts 'No files will be moved' else puts pics2bmoved end On 7/3/06, Dark Ambient wrote: > On 7/3/06, Matthew Smillie wrote: > > > Hmm. This strikes me as a possible misunderstanding of how nested > > loops work. The inner loop (pics2bmoved.each in this case) is > > executed once for every element in the outer loop (destdir.each). In > > a simpler example: > > > > a = ['a', 'b', 'c'] > > b = [1, 2, 3] > > a.each do |letter| > > b.each do |number| > > puts "#{letter}, #{number}" > > end > > end > > > > prints the following: > > a, 1 > > a, 2 > > a, 3 > > b, 1 > > b, 2 > > b, 3 > > c, 1 > > c, 2 > > c, 3 > > Excellent illustration, very helpful , though the results are still forthcoming. > > > Which seems similar to what I guessed your problem might be. Are you > > creating pics2bmoved in a similar sort of nested loop? > > pics2bmoved was created using just .push (which was the first section) > However in section 2, it was the inner loop to the destination > directory / or outer array. > This would explain the repeating for the same element. I've gone > ahead and am attempting to condense the two sections into one. I'm > still faced with the same loop issues though for now. > > Best of luck, > > matthew smillie. > > Stuart > >