Combining Hash elements to produce single line output

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.

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

n Apr 13, 2007, at 1:30 PM, 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"]]

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 Gray II

# 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

···

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

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

Desired Output:
---
"foo" "123" "abc"
"bar" "456" "xyz"
---

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

···

On 13.04.2007 20:26, Paul wrote:

Peter Szinek wrote:

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 }

Sorry this does not answer your question :slight_smile: 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