From: jake kaiden Date: 2011-04-14T08:59:45+09:00 Subject: Re: Help on hashing multiple keys and values Adam Adam wrote in post #992620: > I'm trying to create a hash with multiple values per key from a tab > delimited file. numbers_and_colors.txt example: > >The idiom I use is to add an array to hold the values, like this: >(name_hash[key] ||= []) << value hi adam - i do what clifford does, but since i'm thick-headed i do it a bit more verbosely (is that a word?)... foo = Hash.new{|key, value| key[value] = []} this sets up a hash, where each key's value is an array (you can also get all crazy, and make each value a hash...) once you've got that set, play around with this kind of stuff... foo ["rays"] = %W[alpha beta gamma] foo ["planets"] = %W[mercury venus earth mars] foo ["colors"] = %W[red orange yellow green blue indigo violet] p foo p foo.length p foo ["rays"][1] p foo ["planets"][-1] p foo ["colors"].length - j -- Posted via http://www.ruby-forum.com/.