0

Possible Duplicate:
Ruby Hash with Duplicate Keys?

I defined a hash:

sorted_words = Hash.new(0)
words.each { |word| sorted_words[word]=word.downcase.chars.sort{ |a, b| a.casecmp(b) }.join

Hash does not allow the duplicate keys so if I have keys like cream, scream, scream, it only considers the first two. But I want to also have the third key saved in my Hash with its appropriate value.

This is for an anagram. After the above code I create another hash and, depending on my values from the code, I create multiple arrays with each array having the anagram string.

What could be a solution for such a situation?

Community
  • 1
  • 1
mu_sa
  • 2,685
  • 10
  • 39
  • 58
  • 2
    http://stackoverflow.com/questions/6808801/ruby-hash-with-duplicate-keys – Zabba Oct 10 '12 at 02:53
  • What are you trying to accomplish? The sample code appears to be an experiment, but it's not going to do anything except store the letters of the word in a sorted order and overwrite the key, which you found out. – the Tin Man Oct 10 '12 at 02:57
  • @theTinMan Its for anagram. After this i create another hash and depending on my values from the above code i create multiple arrays with each array having the anagram string. – mu_sa Oct 10 '12 at 03:01
  • Then you need to explain that in your question. Show an example of your data, and an example of what you want for your resulting hash. As is, your question will have a short life. – the Tin Man Oct 10 '12 at 03:02
  • Ok, thanks, i have edited the question and updated it with the description – mu_sa Oct 10 '12 at 03:06

1 Answers1

1

Based on the comment you're looking for an anagram maker, here's the basis for such a beast:

require 'pp'
require 'set'

words = %w[cream scream scream creams]

hash = Hash.new{ |h,k| h[k] = Set.new }

words.each do |w|
  hash[w.downcase.split('').sort] << w.downcase
end

pp hash

=> {["a", "c", "e", "m", "r"]=>#<Set: {"cream"}>,
 ["a", "c", "e", "m", "r", "s"]=>#<Set: {"scream", "creams"}>}

Given a set of words, this will create a hash, where each key is the sorted list of the letters in the word. The value associated with that key is a set of words with the same letters.

Because the value is a set, only unique words are stored.

Once that hash is populated, you'll have a dictionary you can use to quickly look up other words. Take a word, break it down the same way the keys are broken down, and use that as the key in a look-up:

puts hash['creams'.downcase.split('').sort].to_a.join(', ')

outputs:

scream, creams

If duplicate (and redundant) words are needed:

require 'pp'
require 'set'

words = %w[cream creams scream scream]

hash = Hash.new{ |h,k| h[k] = [] }

words.each do |w|
  hash[w.downcase.split('').sort] << w.downcase
end

pp hash

=> {["a", "c", "e", "m", "r"]=>["cream"],
 ["a", "c", "e", "m", "r", "s"]=>["creams", "scream", "scream"]}

puts hash['creams'.downcase.split('').sort].to_a.sort.join(', ')

=> creams, scream, scream
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • considering you words. I require [[cream][creams,scream,scream]]. Currently through my implementation i am acheiving [[cream][creams,scream]]. The scream is not copied twice because of hash rule. – mu_sa Oct 10 '12 at 03:28
  • Why would you want to have duplicate copies of words? Are you counting them? I added a second example with a tiny change that retains duplicates. – the Tin Man Oct 10 '12 at 03:42