From: Harry Kakueki Date: 2007-06-16T22:08:31+09:00 Subject: Re: Adding new value (in array) to existing key in a hash On 6/16/07, Gilbert Lau wrote: > Hi all, > > Am new to programming am learning to use ruby to code a program. How do > I add a new value to existing key without overwriting the old value? > Went to documentation, but didn't find anything that can help me. > Searched on google as well and in vain. > > Say I have a value of coordinate [i,j] and am using a hash table to > store these coordinates for a specific location. Let's say, > > Starbuck => [1,2] > > How to I append or add a new coordinate of [1,7] to Starbuck? to become, > > Starbuck => [1,2], [1,7] > > And able to retrieve each individual coordinates later in array as well. > The idea behind the code is iterate, if new location for Starbuck is > found, add the location in i,j format to key "starbuck". > > Thanks a lot for your help. > > -- > Posted via http://www.ruby-forum.com/. > > If I understood you correctly, try something like this. myhash = {} myhash["Starbuck"] = [] myhash["Starbuck"] << [1,2] myhash["Starbuck"] << [1,7] p myhash p myhash["Starbuck"][1] p myhash["Starbuck"][1][1] Harry -- A Look into Japanese Ruby List in English http://www.kakueki.com/