From: Robert Klemme Date: 2007-03-31T06:00:06+09:00 Subject: Re: newbie, hash or 2dArry On 30.03.2007 18:54, Zac Elston wrote: > I'm lost or just tired from guessing. > > I need to make an hash I can call by hostname as the key and some random > amount of data after that as the values. now a hash works great for 1 > to 1 maps (h[serverX] ==> result) but I need > h[serverX][query|result|time] > > so how would I write a hash that would give me access to > > in: > h[host_to_query]["query"]] = @query > h[host_to_query]["result"]] = queryhost(host_to_query,@query) This is syntactically incorrect since oyu have one closing bracket too much. > out: > for h.each |host| puts h[[host]["result"]] Here's an alternative: Info = Struct.new :query, :result, :time h = Hash.new {|h,k| h[k] = Info.new} h[serverX].query = @query h[serverX].result = queryhost(host_to_query,@query) h.each {|ho,inf| puts inf.result} Of course you can use Arrays instead of Info but the code with Info is more readable and less error prone. Kind regards robert