Merge values of 2 hashes into 1 using Ruby -
i have 2 hashes of hashes need combine on values using ruby
hoh = hash.new hoh = {"bob::params::search_vol_grp"=>{2=>{"search_group_id"=>2, "vol_ids"=>"2,627,628", "num_shards"=>32, "num_replicas"=>1}, 3=>{"search_group_id"=>3, "vol_ids"=>"3,629,630", "num_shards"=>32, "num_replicas"=>1}, 4=>{"search_group_id"=>4, "vol_ids"=>"4,631,632", "num_shards"=>32, "num_replicas"=>1}} hob = hash.new hob = {"bob::params::search_q_broker"=>{2=>{"master_host"=>"hq01p1", "master_port"=>"61616", "slave_host"=>"hq01p2", "slave_port"=>"61616"}, 3=>{"master_host"=>"hq01p1", "master_port"=>"61616", "slave_host"=>"hq01p2", "slave_port"=>"61616"}, 4=>{"master_host"=>"hq01p1", "master_port"=>"61616", "slave_host"=>"hsq01p2", "slave_port"=>"61616"}}
what i'd end following -
hon = hash.new hon = {"bob::params::search"=>{2=>{"master_host"=>"hq01p1", "master_port"=>"61616", "slave_host"=>"hq01p2", "slave_port"=>"61616", "search_group_id"=>2, "vol_ids"=>"2,627,628", "num_shards"=>32, "num_replicas"=>1}, 3=>{"master_host"=>"hq01p1", "master_port"=>"61616", "slave_host"=>"hq01p2", "slave_port"=>"61616", "search_group_id"=>3, "vol_ids"=>"3,629,630", "num_shards"=>32, "num_replicas"=>1}, 4=>{"master_host"=>"hq01p1", "master_port"=>"61616", "slave_host"=>"hq01p2", "slave_port"=>"61616", "search_group_id"=>4, "vol_ids"=>"4,631,632", "num_shards"=>32, "num_replicas"=>1}}
i've tried various attempts @ using merge , merge! none of yielded end result i'm looking for.
any tips?
edit using suggestion larry, able achieve wanted following
hon = hash.new hon['bob::params::search'] = hash.new hoh.each |key,value| value.each |k, v| hon['bob::params::search'][k] = hoh['bob::params::search_vol_grp'][k].merge! (hob['bob::params::search_q_broker'][k]) end end
i updated 2nd hash use fixnum rather string indicated darshan.
thanks pointers guys!
something this...
1[2].merge(2[2])
you need merge inner hashes.
Comments
Post a Comment