Hash merge question

R

Random Qu

Hi All,
I have 2 hashes like this.

h1 = {
a1 => [y1, y2, y3],
a2 => [y4, y5, y6]
}
h2 = {
a2 => [z1, z2, z3],
a3 => [z4, z5, z6]
}

And I nedd to merge them into something like this.

h = {
a1 => [[y1, y2, y3] , []]
a2 => [[y4, y5, y6] , [z1, z2, z3]]
a3 => [[] , [z4, z5, z6]]
}

What's the best way of doing this?
Thanks a lot for your help!
- RQ
 
B

Brian Candler

Random said:
What's the best way of doing this?

Why don't you show us what you've done so far, whether or not it works,
and then we can suggest whether there are improvements which can be
made.

Regards,

Brian.
 
F

Florian Kaufmann

#!/usr/bin/ruby
h1 = {
:a1 => [:y1, :y2, :y3],
:a2 => [:y4, :y5, :y6]
}
h2 = {
:a2 => [:z1, :z2, :z3],
:a3 => [:z4, :z5, :z6]
}
h = {}
(h1.keys + h2.keys).each {|k| h[k] = [[],[]] }
h = h1.to_a.inject(h) { |i,kv| i[kv[0]][0] = kv[1]; i }
h = h2.to_a.inject(h) { |i,kv| i[kv[0]][1] = kv[1]; i }
p h

gives

{:a2=>[[:y4, :y5, :y6], [:z1, :z2, :z3]], :a1=>[[:y1, :y2, :y3],
[]], :a3=>[[], [:z4, :z5, :z6]]}
 
S

Stefan Rusterholz

A little bit simpler:

result = {}
(h1.keys | h2.keys).each { |key| result[key] = [(h1[key] || []),
(h2[key] || []) }

No need to abuse inject.

Regards
Stefan

Florian said:
#!/usr/bin/ruby
h1 = {
:a1 => [:y1, :y2, :y3],
:a2 => [:y4, :y5, :y6]
}
h2 = {
:a2 => [:z1, :z2, :z3],
:a3 => [:z4, :z5, :z6]
}
h = {}
(h1.keys + h2.keys).each {|k| h[k] = [[],[]] }
h = h1.to_a.inject(h) { |i,kv| i[kv[0]][0] = kv[1]; i }
h = h2.to_a.inject(h) { |i,kv| i[kv[0]][1] = kv[1]; i }
p h

gives

{:a2=>[[:y4, :y5, :y6], [:z1, :z2, :z3]], :a1=>[[:y1, :y2, :y3],
[]], :a3=>[[], [:z4, :z5, :z6]]}
 

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
474,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top