From: SpringFlowers AutumnMoon Date: 2007-09-18T22:04:29+09:00 Subject: Re: how to use tuple as hash key Rick Denatale wrote: > On 9/18/07, SpringFlowers AutumnMoon wrote: >> > end >> > end >> >> that is already listed in the initial program. the main requirement is >> that I have a list (an array), and how do I use it as a hash key. > > > But this code does meet this requirement. > > [i,j] IS an array, so > > h2[[i,j]] = "ha" > > is storing "ha" in the hash with a key which is an array containing > references to the objects currently referenced by i and j. my application is this: i have an array with numbers in it. and i will have a shuffle method to shuffle numbers in that array. and then i have to use that array's value as a hash key, so as to h[a] ||= 0 # if nil, then set to 0 h[a] += 1 # to tally up the count (and repeat the shuffling 100,000 times and keep the count) so in this case, i don't think rehash will do the job, as rehash merely look at what "a" currently references to, and rebuild the hash. it will only have 1 key. so what I will do is either to use key = a.dup h[key] ||= 0 h[key] += 1 or even safer: key = a.dup key.freeze h[key] ||= 0 h[key] += 1 so this is like, to use an array that can change as a tuple. -- Posted via http://www.ruby-forum.com/.