addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwchatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgoogleimageimagesinstagramlinklocation-pinmagnifying-glassmailminusmoremuplabelShape 3 + Rectangle 1outlookpersonplusprice-ribbonImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruseryahoo

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

From: Pavan
Sent on: Tuesday, March 9, 2010 7:41 PM
The problem is with
test = {groupid=>{fnam=>­{"lname"=>lname, "bday"=>bday}}}
You are overwriting test. Use

test[groupid] = ...

Best,
Pavan

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.githu...­ below is the same?code:
>
> This my output_buffer that needs to be parsed and presented in a 3D Hash.
> groupid fnam   lname    bday
> 1        abc    xyz     jan1
> ?????????def    lmn     feb1
> 2        amy    morgan  may1
> ?????????nick   cary    may6
> Here is a sample code..
>
> begin
> test = Hash.new
> test[port] = Hash.new
> test[port].store(typ­e, Hash.new)
>
> mybuffer.each { |line|
>
> ????groupid   <- 1
>
> ????fname     <- 'abc'
>
> ????lname     <- 'xyz'
>
> ????bday      <- 'jan1'
>
> ????test = {groupid=>{fnam=>­{"lname"=>lname, "bday"=>bday}}}
> }
>
> puts " FINAL HASH-- #{test} "
>
> end
>
>
>
> --------> When I print the final hash; I get only the last line [ nick   cary    may6]
>
> On Tue, Mar 9, 2010 at 7:28 PM, Paul <[address removed]> wrote:
>>
>> On Tue, Mar 9, 2010 at 6:53 PM, Swarna <[address removed]> wrote:
>>>
>>> Here is a sample code..
>>>
>>> myhash = Hash.new {
>>> my_buffer,each |line|
>>> # use the parser to get the groupid, fname, lname bday
>>> groupid
>>> fnam
>>> lname
>>> bday
>>> myhash = {groupid=>{fnam=>­{"lname"=>lname, "bday"=>bday}}}
>>> }
>>>
>>> Now the problem I am seeing is when i print "myhash" at the end of the function, it just prints only the last line of "mybuffer".
>>>
>>
>> The last line of your each() block, you are assigning a brand new Hash instance to the variable "myhash". ?I think you want that last line there to be:
>> myhash[groupid] = {fnam => { "lname" => lname, "bday" => bday}}
>> ...Paul
>>
>>
>>
>>
>>
>> --
>> Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
>> This message was sent by Paul ([address removed]) from Silicon Valley Ruby Meetup.
>> To learn more about Paul, visit his/her member profile
>> To unsubscribe or to update your mailing list settings, click here
>>
>> Meetup, PO Box 4668 #37895 New York, New York[masked] | [address removed]
>
>
>
>
> --
> Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
> This message was sent by Swarna ([address removed]) from Silicon Valley Ruby Meetup.
> To learn more about Swarna, visit his/her member profile
> To unsubscribe or to update your mailing list settings, click here
>
> Meetup, PO Box 4668 #37895 New York, New York[masked] | [address removed]

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