# Combining Hash elements to produce single line output

**URL:** <https://www.ruby-forum.com/t/combining-hash-elements-to-produce-single-line-output/91962>\
**Category:** Ruby\
**Created:** [April 13, 2007, 8:31pm UTC](https://www.ruby-forum.com/t/combining-hash-elements-to-produce-single-line-output/91962 "2007-04-13T20:31:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [April 13, 2007, 8:31pm UTC](https://www.ruby-forum.com/t/combining-hash-elements-to-produce-single-line-output/91962/1 "2007-04-13T20:31:02Z")

</div>

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”]]

## Desired Output:

## “foo” “123” “abc” “bar” “456” “xyz”

## Sample code:

content\_alpha = ‘’  
content\_num = ‘’  
tmp\_hash.sort.each do |key, value|  
(name, num) = key.split(’:’)

# 

content\_alpha = value if ( num == ‘1’ )  
content\_num = value if ( num == ‘2’ )

# 

## print name + “\t” + content\_num + “\t” + content\_alpha end

I know the above doesn’t produce the desired output but I haven’t been  
able to crack it yet. (I’ve tried several different solutions but am  
still new to Ruby and scripting in general.) I was hoping someone  
might be able to provide me with some new suggestions to try.

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [April 13, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/combining-hash-elements-to-produce-single-line-output/91962/2 "2007-04-13T20:56:06Z")

</div>

Paul wrote:

> [“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](http://www.rubyrailways.com) :: Ruby and Web2.0 blog  
[http://scrubyt.org](http://scrubyt.org) :: Ruby web scraping framework  
[http://rubykitchensink.ca/](http://rubykitchensink.ca/) :: The indexed archive of all things Ruby

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [April 13, 2007, 9:12pm UTC](https://www.ruby-forum.com/t/combining-hash-elements-to-produce-single-line-output/91962/3 "2007-04-13T21:12:13Z")

</div>

n Apr 13, 2007, at 1:30 PM, Paul wrote:

> [“bar:1”, “456”],  
> [“bar:2”, “xyz”]]
> 
> ## Desired Output:
> 
> ## “foo” “123” “abc” “bar” “456” “xyz”

> > tmp\_hash =  
> > ?\> [[“foo:1”, “123”],  
> > ?\> [“foo:2”, “abc”],  
> > ?\> [“bar:1”, “456”],  
> > ?\> [“bar:2”, “xyz”]]  
> > =\> [[“foo:1”, “123”], [“foo:2”, “abc”], [“bar:1”, “456”], [“bar:2”,  
> > “xyz”]]
> 
> > require “enumerator”  
> > =\> true
> 
> > tmp\_hash.each\_slice(2) do |alpha, num|  
> > ?\> puts [alpha.first[/^\w+/],  
> > ?\> alpha.last,  
> > ?\> num.last ].map { |f| %Q{"#{f}"} }.join("\t")
> 
> > end  
> > “foo” “123” “abc”  
> > “bar” “456” “xyz”  
> > =\> nil

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [April 13, 2007, 9:13pm UTC](https://www.ruby-forum.com/t/combining-hash-elements-to-produce-single-line-output/91962/4 "2007-04-13T21:13:14Z")

</div>

On Sat, Apr 14, 2007 at 03:30:05AM +0900, Paul wrote:

> * * *

# This is a hash which auto-creates non-existing members as an empty

array  
new\_hash = Hash.new { |h,k| h[k] = [] }

tmp\_hash.each do |key, value|  
key, rest = key.split(’:’, 2)  
new\_hash[key] \<\< value  
end

new\_hash.each do |key, values|  
puts ‘"’ + ([key] + values).join(’" “’) + '”’  
end

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [April 13, 2007, 11:50pm UTC](https://www.ruby-forum.com/t/combining-hash-elements-to-produce-single-line-output/91962/5 "2007-04-13T23:50:27Z")

</div>

On 13.04.2007 20:26, Paul wrote:

> [“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

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [April 14, 2007, 12:07am UTC](https://www.ruby-forum.com/t/combining-hash-elements-to-produce-single-line-output/91962/6 "2007-04-14T00:07:30Z")

</div>

Peter S. wrote:

> > [“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 }

Sorry this does not answer your question 🙂 It confused me that the  
original array was named ‘hash’ so for some reason I have thought you  
need a hash…

Cheers,  
Peter  
\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com) :: Ruby and Web2.0 blog  
[http://scrubyt.org](http://scrubyt.org) :: Ruby web scraping framework  
[http://rubykitchensink.ca/](http://rubykitchensink.ca/) :: The indexed archive of all things Ruby
