Combining Hash elements to produce single line output

P

Paul

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.
 
P

Peter Szinek

Paul said:
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
 
J

James Edward Gray II

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"
---
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"]]?> puts [ alpha.first[/^\w+/],
?> alpha.last,
?> num.last ].map { |f| %Q{"#{f}"} }.join("\t")"foo" "123" "abc"
"bar" "456" "xyz"
=> nil

Hope that helps.

James Edward Gray II
 
B

Brian Candler

tmp_hash =
[["foo:1", "123"],
["foo:2", "abc"],
["bar:1", "456"],
["bar:2", "xyz"]]

Desired Output:

# 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
 
R

Robert Klemme

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:

Kind regards

robert
 
P

Peter Szinek

Peter said:
Paul said:
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 }

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 :: Ruby and Web2.0 blog
http://scrubyt.org :: Ruby web scraping framework
http://rubykitchensink.ca/ :: The indexed archive of all things Ruby
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top