Re: [ruby-99] Nested Hashes/Hash of Hashes in Ruby

From: Paul
Sent on: Tuesday, March 9, 2010 10:28 PM
On Tue, Mar 9, 2010 at 7:37 PM, Swarna <[address removed]> wrote:
my bad.. ��I am parsing the buffer; not the brand new hash table:
http://gist.github.com/327468����or below is the same��code:

begin
test = Hash.new
test[port] = Hash.new
test[port].store(type, Hash.new)

Okay, at this point, "test" has been defined....
��
mybuffer.each { |line|

��������groupid <- 1
��������fname <- 'abc'
��������lname <- 'xyz'
��������bday <- 'jan1'
��������test = {groupid=>{fnam=>{"lname"=>lname, "bday"=>bday}}}

And in this last line, you are *resetting* the value of "test" to a brand new Hash, containing only the key groupid. ��"test" no longer points to the Hash you set it to originally, because the assignment is a completely new value. ��This is why you only ever see the last line of your input buffer; every line, "test" gets re-assigned to a brand new Hash, so when the loop exits, "test" is pointing to a Hash that ONLY contains the last line of your input.

Change this line above to: ��test[groupid] = { fnam => { "lname" => lname, "bday" => bday }}
and you should find at the end, "test" is a Hash containing more than one groupid as a key.

...Paul

��

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy