From: Gordon Thiesfeld Date: 2007-08-17T23:59:58+09:00 Subject: Re: MultiValuedHash > many thanks for the trick > > But have anybody an advice how i get the filenames a.txt, b.txt .... > as key and the parts 00_a.txt , 01_a.txt, 02_a.txt as values for > every key. > > i identify a mistake to get the unique names i have to: > Dir.entries("f:/base_data/ADS20/outputdaten/").each { |e| > files.push(e[3..50])} > filenames.push(files.select{|e| files.index(e) != > files.rindex(e)}.uniq) > If you use the hash idiom Robert showed you, you won't have to worry about checking for uniqueness, because all keys are unique. They'll be created the first time you use them, and updated each time after that. So, the filename is your key, and the file parts get added to the array referenced by that key. I also used e[3..-1] instead of e[3..50]. This will return the string from the 3rd character to the end of the string, regardless of length. hash = Hash.new {|h,k| h[k] = []} Dir.entries(".").each do |e| filename = e[3..-1] hash[filename].push e end