Convert multidimensional array to array of hashes - ruby

I have this data:
input = [ [ 'abc', '1.1' ], [ 'abc', '1.2' ], [ 'xyz', '3.14' ] ]
I would like output like the following:
[ { 'abc' => [ '1.1', '1.2' ] }, { 'xyz' => '3.14' } ]
Is it possible to achieve this in one chained expression?

I'd do it like this:
input = [['abc', '1.1'], ['abc','1.2'], ['xyz', '3.14']]
output = input.each_with_object(Hash.new{ |h, k| h[k] = [] }) { |(k, v), h| h[k] << v }
output # => {"abc"=>["1.1", "1.2"], "xyz"=>["3.14"]}
An alternate, which isn't as straightforward is:
input.group_by{ |k,v| k }.map{ |k, v| [k, v.map(&:last)] }.to_h # => {"abc"=>["1.1", "1.2"], "xyz"=>["3.14"]}
Your output structure
output: [{'abc' => ['1.1', '1.2']}, {'xyz' => '3.14'}]
is a very poor way to use a hash. Instead, you should have one hash, with multiple elements, since you're combining the like-key's values into one key.
If you REALLY need the output that way, as some seem to think, then simply append a map to the returned value:
input.each_with_object(Hash.new{ |h, k| h[k] = [] }) { |(k, v), h| h[k] << v }.map{ |k, v| {k => v} }
# => [{"abc"=>["1.1", "1.2"]}, {"xyz"=>["3.14"]}]

Simplest way I could think of:
input.group_by {|x| x[0]}.each_pair.map { |k, v| {k => (a=v.map(&:last)).count > 1 ? a : a[0]} }

The simplest way to do it is like this:
def convert_marray_to_hash(input)
hash = Hash.new { |hash, key| hash[key] = [] }
output = []
input.each { |array| hash[array[0]] << array[1] }
hash.keys.each { |key| output << { key => hash[key] } }
output
end
There are many ways to do it, but that way presents itself nicely and is readable.

This works pretty well:
input = [ [ 'abc', '1.1' ], [ 'abc','1.2' ], [ 'xyz', '3.14' ] ]
input.each_with_object({}) do |(key, val), hsh|
hsh[key] = val and next unless hsh.key?(key)
hsh[key] = [ *hsh[key], val ]
end
.map {|key, val| { key => val } }
# => [ { "abc" => [ "1.1", "1.2" ] },
# { "xyz" => "3.14" }
# ]
If you omit the final map you end up with a hash, which seems like a more natural result, but this matches the output specified, at least.

Related

How to create a hash from a multidimensional array

I have these params :
x=["room_adults_1", "room_childs_1", "room_adults_2", "room_childs_2"]
And when I run this code:
y = x.map { |x| x.match(/^(room)_(adults|childs)_(\d+)/)}
z = y.map { |x| [x[1],[x[2],[x[3].reverse,""]]]}
I get this array:
=> [["room", ["adults", ["1", ""]]], ["room", ["childs", ["1", ""]]], ["room", ["adults", ["2", ""]]], ["room", ["childs", ["2", ""]]]]
I would like to transform this last result into a Hash. If I use z.to_h (collapse last element), I obtain {"room"=>["childs", ["2", ""]]}. But I would like a Hash like this:
{
"room":{
"adults":{[
{"1": ""},
{"2": ""}
]},
"child":{[
{"1": ""},
{"2": ""}
]}
}
}
How can I do?
x.map { |e| e.split '_' }
.group_by(&:shift) # room
.map { |k, v| [k, v.group_by(&:shift) # adults/children
.map { |k, v| [k, v.map { |e, *| {e => ""} } ] }
.to_h ] }
.to_h
#⇒ {"room"=>{"adults"=>[{"1"=>""}, {"2"=>""}],
# "childs"=>[{"1"=>""}, {"2"=>""}]}}
In this case I prefer each_with_object:
x = ["room_adults_1", "room_childs_1", "room_adults_2", "room_childs_2"]
export = x.each_with_object(Hash.new { |k, v| k[v] = Hash.new { |k, v| k[v] = [] } }) do |string, exp|
room, type, id = string.split("_")
exp[room][type] << {id => ""}
end
p export
# => {"room"=>{"adults"=>[{"1"=>""}, {"2"=>""}], "childs"=>[{"1"=>""}, {"2"=>""}]}}
I assume the desired result is
{:room=>{:adults=>[{:"1"=>""}, {:"2"=>""}], :childs=>[{:"1"=>""}, {:"2"=>""}]}}
as the construct given in the question is not a valid Ruby object, let alone a hash.
arr = ["room_adults_1", "room_childs_1", "room_adults_2", "room_childs_2"]
h = arr.map { |s| s.split('_')[1,2].map(&:to_sym) }.group_by(&:first)
#=> {:adults=>[[:adults, :"1"], [:adults, :"2"]],
# :childs=>[[:childs, :"1"], [:childs, :"2"]]}
{ room: h.merge(h) { |k,a,_| a.map { |_,b| { b=>"" } } } }
#=> {:room=>{:adults=>[{:"1"=>""}, {:"2"=>""}], :childs=>[{:"1"=>""}, {:"2"=>""}]}}
If the keys are to be strings, not symbols, remove .map(&:to_sym).

How to convert, in Ruby, a list of key value pairs to a Hash, such that values with duplicate keys are stored in an array?

Given a list key-value pairs, in the form of an array of arrays - e.g. [ ["key1","value1"], ["key2","value2"], ["key1", "value3"] ], how to convert these to a Hash that stores all the values, in the most elegant way?
For the above example, I would want to get { "key1" => [ "value1", "value3" ], "key2" => [ "value2" ] }.
[["key1","value1"], ["key2","value2"], ["key1", "value3"]]
.group_by(&:first).each{|_, v| v.map!(&:last)}
Another way is to use the form of Hash#update (aka merge!) that uses a block to determine the values of keys that are in both hashes being merged.
arr = [ ["key1","value1"], ["key2","value2"], ["key1", "value3"] ]
arr.each_with_object({}) { |(k,v),h| h.update(k=>[v]) { |_,o,n| o+n } }
#=> {"key1"=>["value1", "value3"], "key2"=>["value2"]}
Hash.new{ |h,k| h[k]=[] }.tap{ |h| array.each{ |k,v| h[k] << v } }
OR
c = Hash.new {|h,k| h[k] = [] }
array.each{|k, v| c[k] << v}
My best solution so far is this:
kvlist.inject(Hash.new([])) do |memo,a|
memo[a[0]] = (memo[a[0]] << a[1])
memo
end
Which I think is not very good.

How to map a Ruby hash?

Is there a better way to map a Ruby hash? I want to iterate over each pair and collect the values. Perhaps using tap?
hash = { a:1, b:2 }
output = hash.to_a.map do |one_pair|
k = one_pair.first
v = one_pair.last
"#{ k }=#{ v*2 }"
end
>> [
[0] "a=2",
[1] "b=4"
]
Ruby's hash includes the Enumerable module which includes the map function.
hash = {a:1, b:2}
hash.map { |k, v| "#{k}=#{v * 2}" }
Enumerable#map | RubyDocs
Err, yes, with map, invoked directly on the hash:
{ a:1, b:2 }.map { |k,v| "#{k}=#{v*2}" } # => [ 'a=2', 'b=4' ]
hash = { a:1, b:2 }
hash.map{|k,v| [k,v*2]* '='}
# => ["a=2", "b=4"]

Best way to merge key value pairs in a hash based on number of values for that key in Ruby

I have a hash of arrays in ruby as :
#people = { "a" => ["john", "mark", "tony"], "b"=> ["tom","tim"],
"c" =>["jane"], "others"=>["rob", "ryan"] }
I would like to merge all key value pairs where there are less than 3 items in the array for a particular keys values. They should be merged into the key called "others" to give roughly the result of
#people = { "a" => ["john", "mark", "tony"],
"others"=> ["rob", "ryan", "tom", "tim", "jane"] }
Using the following code is problematic as duplicate key values in a hash cannot exist:
#people = Hash[#people.map{|k,v| v.count<3 ? ["others",v] : [k,v]} ] %>
Whats the best way to elegantly solve this?
You almost have it, the problem is, as you notice, that you can't build the Hash's key/value pairs on the fly because of duplicates. One way around the problem is to start out with the skeleton of what you're trying to build:
#people = #people.each_with_object({ 'others' => [ ] }) do |(k,v), h|
if(v.length >= 3)
h[k] = v
else
h['others'] += v
end
end
Or, if you don't like each_with_object, you could:
h = { 'others' => [ ] }
#people.each do |k, v|
# as above
end
#people = h
Or you could use pretty much the same structure with inject (taking care, as usual, to return the right thing from the block).
There are certainly other ways to do this but these approaches are pretty clear and easy to understand; IMO clarity should be your first goal.
try:
>> #people = { "a" => ["john", "mark", "tony"], "b"=> ["tom","tim"],
"c" =>["jane"], "others"=>["rob", "ryan"] }
>> #new_people = {"others" => []}
>> #people.each_pair {|k,v| (v.size >= 3 && k!="others") ? #new_people.merge!(k=>v) : #new_people['others']+= v}
>> #new_people
=> {"others"=>["rob", "ryan", "jane", "tom", "tim"], "a"=>["john", "mark", "tony"]}
Hash[ #people.group_by { |k,v| v.size < 3 ? 'others' : k }.
map { |k,v| [k, v.flat_map(&:last)] } ]
=> {"a"=>["john", "mark", "tony"],
"others"=>["tom", "tim", "jane", "rob", "ryan"]}
What about this:
> less_than_three, others = #people.partition {|(key, values)| values.size >= 3 }
> Hash[less_than_three]
# => {"a"=>["john", "mark", "tony"]}
> Hash["others" => others.map {|o| o.last}.flatten]
# => {"others"=>["tom", "tim", "jane", "rob", "ryan"]}
#people[:others] = []
#people.each do |k, v|
#people[:others] |= #people.delete(k) if v.size < 3
end
#people.inject({}) do |m, (k, v)|
m[i = v.size >= 3 ? k : 'others'] = m[i].to_a + v
m
end

ruby db result set to array in a hash in a hash

I have a db query which returns results like:
db_result.each {|row| puts row}
{"IP"=>"1.2.3.4","Field1"=>"abc","Field2"=>"123"}
{"IP"=>"1.2.3.4","Field1"=>"abc","Field2"=>"234"}
{"IP"=>"1.2.3.4","Field1"=>"bcd","Field2"=>"345"}
{"IP"=>"3.4.5.6","Field1"=>"bcd","Field2"=>"456"}
{"IP"=>"3.4.5.6","Field1"=>"bcd","Field2"=>"567"}
And want to put it into a hash like:
{
"1.2.3.4" => {
"abc" => ["123", "234"],
"bcd" => "345"
},
"3.4.5.6" => {
"bcd" => ["456", "567"]
}
}
What I am currently doing is:
result_hash = Hash.new { |h, k| h[k] = {} }
db_result.each do |row|
result_hash[row["IP"]] = Hash.new { |h, k| h[k] = [] } unless result_hash.has_key? row["IP"]
result_hash[row["IP"]][row["Field1"]] << row["Field2"]
end
Which works, however was wondering if there is a neater way.
Consider this a peer-review. As a recommendation for processing and maintenance...
I'd recommend the data structure you want be a little more consistent.
Instead of:
{
"1.2.3.4" => {
"abc" => ["123", "234"],
"bcd" => "345"
},
"3.4.5.6" => {
"bcd" => ["456", "567"]
}
}
I'd recommend:
{
"1.2.3.4" => {
"abc" => ["123", "234"],
"bcd" => ["345"]
},
"3.4.5.6" => {
"abc" => [],
"bcd" => ["456", "567"]
}
}
Keep the same keys in each sub-hash, and make the values all be arrays. The code for processing that overall hash will be more straightforward and easy to follow.
I agree with Michael, there is nothing wrong with your method. The intent behind the code can be easily seen.
If you want to get fancy, here's one (of many) ways to do it:
x = [
{"IP"=>"1.2.3.4","Field1"=>"abc","Field2"=>"123"},
{"IP"=>"1.2.3.4","Field1"=>"abc","Field2"=>"234"},
{"IP"=>"1.2.3.4","Field1"=>"bcd","Field2"=>"345"},
{"IP"=>"3.4.5.6","Field1"=>"bcd","Field2"=>"456"},
{"IP"=>"3.4.5.6","Field1"=>"bcd","Field2"=>"567"}
]
y = x.inject({}) do |result, row|
new_row = result[row["IP"]] ||= {}
(new_row[row["Field1"]] ||= []) << row["Field2"]
result
end
I think this should yield the same time complexity as your method.

Resources