tags:

views:

50

answers:

1

So I'm trying to build a hash from an array. Basically I want to take the unique string values of the array and build a hash with a key. I'm also trying to figure out how to record how many times that unique word happens.

        #The text from the .txt file:

        # **Bob and George are great! George and Sam are great.
        #Bob, George, and sam are great!**

        #The source code:
        count_my_rows = File.readlines("bob.txt")
        row_text = count_my_rows.join

        puts row_text.split.uniq #testing to make sure array is getting filled

Anyways I've tried http://ruby-doc.org/core/classes/Hash.html

I think I need to declare a empty hash with name.new to start I have no idea how to fill it up though. I'm assuming some iteration through the array fills the hash. I'm starting to think I need to record the value as a seperate array storing the time it occurs and the word then assign the hash key to it. Example = { ["Bob",2] => 1 , ["George",3], =>2 } Leave some code so I can mull over it, thanks for your help.

A: 

To get you started,

h={}
h.default=0
File.read("myfile").split.each do |x|
  h[x]+=1
end
p h

Note: this is not complete solution