From: Robert Klemme Date: 2007-04-14T06:50:06+09:00 Subject: Re: Combining Hash elements to produce single line output On 13.04.2007 20:26, Paul wrote: > Hi there. I know I've done something similar to this before but I > seem to be stumped at the moment and thought I would ask for help. > > I have a temporary Hash array that holds information in different > elements that need to be combined to produce single line outputs. > Here's some detail: > > tmp_hash = > [["foo:1", "123"], > ["foo:2", "abc"], > ["bar:1", "456"], > ["bar:2", "xyz"]] This is not a Hash. I suggest you choose a more appropriate data structure, use each key only once and stuff all values in an Array, i.e. tmp = { "foo" => %w{123 abc}, "bar" => %w{456 xyz} } or tmp = [ ["foo", %w{123 abc}], ["bar", %w{456 xyz}] ] and then tmp.each do |h,k| print h.inspect, " ", k.map {|kk| kk.inspect}.join(" "), "\n" end or even tmp.each {|a| print '"', a.flatten.join('" "'), "\"\n"} > Desired Output: > --- > "foo" "123" "abc" > "bar" "456" "xyz" > --- Kind regards robert