From: Robert Klemme Date: 2003-08-08T00:26:08+09:00 Subject: Re: Elegant solution for a loop-break problem "KONTRA Gergely" schrieb im Newsbeitrag news:20030807141710.GE7040@mlabdial.hit.bme.hu... > Hi! > > I've tried to write a little program, which detect duplicate files. > My way to solve this is to have a hash for the known files like this > { fileSize => [[file1,file2],[file3,file4]], > fileSize2 => [[file6,file7]] } > > So, file1..file4 have the same size, file1 and file2's content is > identical, similarly file3 and file4 is identical. > > First, the Hash is empty, and then I insert the files to be examined, > like this. > > files = Hash.new { [] } > for filename in filesToBeAdded > filesize=File.filesize(filename) > for fn in filename[filesize] > if equalContent(fn[0], filename) > # add this file to this 'segment' TODO: alter files > # next 'filename in filesToBeAdded' > end > end > # if loop exited normally, create a new segment > end Often the best solution is to use modularization for such problems. It makes code more readable and easier to maintain: def addToGroup(group, filename) for fn in group if equalContent(fn[0], filename) # add this file to this 'segment' TODO: alter files # next 'filename in filesToBeAdded' fn << filename return end end # if loop exited normally, create a new segment group << [filename] end # or, a bit more rubish def addToGroup(group, filename) group.each do |fn| if equalContent(fn[0], filename) # add this file to this 'segment' TODO: alter files # next 'filename in filesToBeAdded' fn << filename return end end # if loop exited normally, create a new segment group << [filename] end # main files = Hash.new { [] } for filename in filesToBeAdded filesize=File.filesize(filename) addToGroup( files[ filesize ] ) end > So questions in general: > How can I alter items in a hash nicely? hash[ key ]. > Can I exit more loops with next/last/redo? (like in perl) As others pointed out not in all versions of ruby. > Is there something similar to python's for .. else ... structure? (Do > something, ONLY when the loop is terminated normally (without break) Dunno. robert