From: Peter Szinek Date: 2007-04-14T03:55:39+09:00 Subject: Re: Combining Hash elements to produce single line output 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"]] > Whew, this will be ugly... I am sure somebody has a more effective solution, but until then: to_hash = tmp_hash.inject(Hash.new('')) {|hash, arr| key = arr[0].scan(/(.+):/)[0][0] hash[key] = hash[key]+(val = hash[key] == '' ? '' : ',')+arr[1] hash } Cheers, Peter __ http://www.rubyrailways.com :: Ruby and Web2.0 blog http://scrubyt.org :: Ruby web scraping framework http://rubykitchensink.ca/ :: The indexed archive of all things Ruby