Removing nils is quite simple, however, I'd like to know:
1) What am I doing wrong, why does my array result below include nils?
2) How I can PREVENT the nils from being added to my array, instead of removing them after the fact.
#cars = Array.new
plucked_array = [
[8, "Chevy", "Camaro", 20],
[9, "Ford", "Mustang", 55],
[9, "Ford", "Fusion", 150]
]
plucked_array.
each { |id, make, model, model_count|
#cars[id] ||= {name: make, id: make, data: []}
#cars[id][:data].push([model, model_count])
}
puts #cars.inspect
#=>[nil, nil, nil, nil, nil, nil, nil, nil, {:name=>"Chevy", :id=>"Chevy", :data=>[["Camaro", 20]]}, {:name=>"Ford", :id=>"Ford", :data=>[["Mustang", 55], ["Fusion", 150]]}]
puts #cars.compact.inspect
#=>[{:name=>"Chevy", :id=>"Chevy", :data=>[["Camaro", 20]]}, {:name=>"Ford", :id=>"Ford", :data=>[["Mustang", 55], ["Fusion", 150]]}]
# This gives the result I'm looking for,
# just wondering how to best get transformed array without the post-cleanup.
I also attempted #theTinMan's recommendation to select first, then map, but I had the same results:
plucked_array.select { |id, make, model, model_count|
#cars[id] = {'name' => make, 'id' => make, 'data' => []}
}.map { |id, make, model, model_count|
#cars[id]['data'].push([model, model_count])
}
puts #cars.inspect
#=>[nil, nil, nil, nil, nil, nil, nil, nil, {:name=>"Chevy", :id=>"Chevy", :data=>[["Camaro", 20]]}, {:name=>"Ford", :id=>"Ford", :data=>[["Mustang", 55], ["Fusion", 150]]}]
I've tried, with partial success, to use Hash instead of an array for #cars. This prevented nils, but my end goal is to build "drilldown: series[]" below, which is an array of hashes:
// Create the chart
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: 'Imaginary Car Stats'
},
subtitle: {
text: 'Click the columns to view models.'
},
xAxis: {
type: 'category'
},
yAxis: {
title: {
text: 'Total car count'
}
},
legend: {
enabled: false
},
plotOptions: {
series: {
borderWidth: 0,
dataLabels: {
enabled: true,
format: '{point.y}'
}
}
},
tooltip: {
headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
},
/*I have separate `pluck` query for this top-level series:*/
"series": [
{
"name": "Cars",
"colorByPoint": true,
"data": [
{
"name": "Ford",
"y": 205,
"drilldown": "Ford"
},
{
"name": "Chevy",
"y": 20,
"drilldown": "Chevy"
},
{
"name": "Other",
"y": 16,
"drilldown": null
}
]
}
],
"drilldown": {
/*This is the array of hashes I'm attempting to create:*/
"series": [
{
"name": "Ford",
"id": "Ford",
"data": [
[
"Fusion",
150
],
[
"Mustang",
55
]
]
},
{
"name": "Chevy",
"id": "Chevy",
"data": [
[
"Camaro",
20
]
]
}
]
}
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/data.js"></script>
<script src="https://code.highcharts.com/modules/drilldown.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
The reason your code is behaving the way it does is because array[8] = x inserts x at position 8 in the array, and ruby fills the spaces up to 8 with nils.
a = []
a[7] = 4
a == [nil, nil, nil, nil, nil, nil, nil, 4]
You need #cars to be a hash - not an array
I think this will do what you are trying to do:
plucked_array = [
[8, "Chevy", "Camaro", 20],
[9, "Ford", "Mustang", 55],
[9, "Ford", "Fusion", 150]
]
cars = plucked_array.each_with_object({}) do |(id, make, model, count), cars|
cars[id] ||= {id: id, make: make, data: []}
cars[id][:data] << [model, count]
end
p cars.values
Which actually is almost identical to #Austio's solution.
I would go for more of a reduce approach because you are taking a list and boiling it down into another object. each_with_object does the reduce but implicitly returns the obj (in this case cars) in every loop
new_list = plucked_array.each_with_object({}) do |(id, make, model, model_count), cars|
# return before mutating cars hash if the car info is invalid
cars[id] ||= {name: make, id: make, data: []}
cars[id][:data].push([model, model_count])
end
# Then in your controller to handle the usage as an array
#cars = new_list.values
Side note: map is normally more for transforms or equivalent changes, which is i think why it feels off here.
I suggest grouping by id, then creating a hash:
plucked_array.group_by(&:first).transform_values{ |v| v.map{ |id, make, model, model_count| {name: make, id: make, data: [model, model_count]} }}
# {8=>[{:name=>"Chevy", :id=>"Chevy", :data=>["Camaro ls", 20]}],
# 9=>[{:name=>"Ford", :id=>"Ford", :data=>["Mustang", 55]}, {:name=>"Ford", :id=>"Ford", :data=>["Fusion", 150]}]}
# }
Edit - update do match the edit of the original question (get series of data for Highcharts)
This should return the required result:
plucked_array.map.with_object(Hash.new([])) { |(id, make, model, model_count), h|
h[make] += [[model, model_count]] }.map { |k, v| {name: k, id: k, data: v }}
#=> [{:name=>"Chevy", :id=>"Chevy", :data=>[["Camaro", 20]]}, {:name=>"Ford", :id=>"Ford", :data=>[["Mustang", 55], ["Fusion", 150]]}]
The first part builds an hash like this.
#=> {"Chevy"=>[["Camaro", 20]], "Ford"=>[["Mustang", 55], ["Fusion", 150]]}
Passing Hash.new([]) as object allows to insert elements to the default array.
Finally the hash is mapped to the required keys.
I am trying to turn an array of arrays into an array of hashes. Could someone try to explain what I am exactly doing wrong here? The first array within the array of arrays becomes the keys for the hash. I can get the method to return one hash or even three of the same hashes. But I can't get it return each different new hash within the final array.
table_data = [
["first_name", "last_name", "city", "state"],
["Elisabeth", "Gardenar", "Toledo", "OH"],
["Jamaal", "Du", "Sylvania", "OH"],
["Kathlyn", "Lavoie", "Maumee", "OH"]
]
def convert_table(table_array)
hash = {}
final_array = []
headers_array = table_array.shift
table_array.each_index do |x|
i = 0
until i == headers_array.length
hash[headers_array[i]] = table_array[x][i]
final_array << hash
i += 1
end
end
final_array
end
p convert_table(table_data)
#END GOAL
[ { "first_name" => "Elisabeth", "last_name" => "Gardenar", "city" => "Toledo", "state" => "OH" },
{ "first_name" => "Jamaal", "last_name" => "Du", "city" => "Sylvania", "state" => "OH" },
{ "first_name" => "Kathlyn", "last_name" => "Lavoie", "city" => "Maumee", "state" => "OH" }
Pair up keys (in table_data[0] and values (each other row in table_data) using zip, and map them to a hash:
table_data[1..-1].map { |values| Hash[table_data[0].zip(values)] }
EDIT: The part that doesn't work in your case is having one single hash that you keep reusing. When you do final_array << hash, it doesn't add a snapshot of the hash as it is then; it adds a reference to it. Thus, you don't have an array of three hashes, you have an array with three references to the same hash. You could avoid it by doing final_array << hash.clone to actually take a snapshot; or (much simpler) just make a new hash in each iteration of the loop (move hash = {} into the table_array.each_index loop).
As #Amadan has diagnosed your problem, I will suggest a more "Ruby-like" approach.
keys, *data = table_data
#=> [["first_name", "last_name", "city", "state"],
# ["Elisabeth", "Gardenar", "Toledo", "OH"],
# ["Jamaal", "Du", "Sylvania", "OH"],
# ["Kathlyn", "Lavoie", "Maumee", "OH"]
# ]
keys
#=> ["first_name", "last_name", "city", "state"]
data
#=> [["Elisabeth", "Gardenar", "Toledo", "OH"],
# ["Jamaal", "Du", "Sylvania", "OH"],
# ["Kathlyn", "Lavoie", "Maumee", "OH"]
# ]
[keys].product(data).map { |pair| pair.transpose.to_h }
#=> [{"first_name"=>"Elisabeth", "last_name"=>"Gardenar", "city"=>"Toledo",
# "state"=>"OH"},
# {"first_name"=>"Jamaal", "last_name"=>"Du", "city"=>"Sylvania",
# "state"=>"OH"},
# {"first_name"=>"Kathlyn", "last_name"=>"Lavoie", "city"=>"Maumee",
# "state"=>"OH"}
# ]
The steps are as follows.
a = [keys].product(data)
#=> [[["first_name", "last_name", "city", "state"],
# ["Elisabeth", "Gardenar", "Toledo", "OH"]
# ],
# [["first_name", "last_name", "city", "state"],
# ["Jamaal", "Du", "Sylvania", "OH"]],
# [["first_name", "last_name", "city", "state"],
# ["Kathlyn", "Lavoie", "Maumee", "OH"]
# ]
# ]
The first element of a is passed to map, the block variable pair is assigned and the block calculation is performed.
pair = a.first
#=> [["first_name", "last_name", "city", "state"],
# ["Elisabeth", "Gardenar", "Toledo", "OH"]
# ]
b = pair.transpose
#=> [["first_name", "Elisabeth"],
# ["last_name", "Gardenar"],
# ["city", "Toledo"],
# ["state", "OH"]
# ]
g = b.to_h
#=> {"first_name"=>"Elisabeth", "last_name"=>"Gardenar", "city"=>"Toledo",
# "state"=>"OH"}
Therefore, a.first is mapped to g. The remaining calculations are similar.
Your question has been properly answered by #CarySwoveland and #Amadan.
I'd just like to add that your table basically looks like a CSV table with header.
If your table_data does come from a file, you might as well read it directly with CSV :
csv_table = "first_name,last_name,city,state
Elisabeth,Gardenar,Toledo,OH
Jamaal,Du,Sylvania,OH
Kathlyn,Lavoie,Maumee,OH"
require 'csv'
CSV.parse(csv_table, headers: true).each do |row|
p row
end
It outputs
#<CSV::Row "first_name":"Elisabeth" "last_name":"Gardenar" "city":"Toledo" "state":"OH">
#<CSV::Row "first_name":"Jamaal" "last_name":"Du" "city":"Sylvania" "state":"OH">
#<CSV::Row "first_name":"Kathlyn" "last_name":"Lavoie" "city":"Maumee" "state":"OH">
You can work CSV::Row as with an Hash.
If you really want a Hash, you can use row.to_h :
{"first_name"=>"Elisabeth", "last_name"=>"Gardenar", "city"=>"Toledo", "state"=>"OH"}
{"first_name"=>"Jamaal", "last_name"=>"Du", "city"=>"Sylvania", "state"=>"OH"}
{"first_name"=>"Kathlyn", "last_name"=>"Lavoie", "city"=>"Maumee", "state"=>"OH"}
I have a ruby array of 3 hashes. Each peace has information about report_data (consumption of 2 types of energy) and monthes_data (same for each). Please see the code below.
arr = [{:report_data=>
[{:type=>
{"id"=>1, "name"=>"electricity"},
:data=>[10, 20, 30, 40]},
{:type=>
{"id"=>2, "name"=>"water"},
:data=>[20, 30, 40, 50]}],
:monthes_data=>
{:monthes=>
["jan", "feb"]},
{:report_data=>
[{:type=>
{"id"=>1, "name"=>"electricity"},
:data=>[15, 25, 35, 45]},
{:type=>
{"id"=>2, "name"=>"water"},
:data=>[25, 35, 45, 55]}],
:monthes_data=>
{:monthes=>
["jan", "feb"]},
{:report_data=>
[{:type=>
{"id"=>1, "name"=>"electricity"},
:data=>[17, 27, 37, 47]},
{:type=>
{"id"=>2, "name"=>"water"},
:data=>[27, 37, 47, 57]}],
:monthes_data=>
{:monthes=>
["jan", "feb"]}]
I'm new to Ruby. Please help me to sum all the data by energy types. In the end I want to have one hash with report_data and monthes_data. I need the result look like:
{:report_data=>
[{:type=>
{:"id"=>1, "name"=>"electricity"},
:data=>[42, 72, 102, 132]},
{:type=>
{"id"=>2, "name"=>"water"}},
:data=>[72, 102, 132, 162]}],
:monthes_data=>
{:monthes=>
["jan", "feb"]}}
arr = [{:report_data=>
[{:type=>
{"id"=>1, "name"=>"electricity"},
:data=>[10, 20, 30, 40]},
{:type=>
{"id"=>2, "name"=>"water"},
:data=>[20, 30, 40, 50]}],
:monthes_data=>
{:monthes=>
["jan", "feb"]}},
{:report_data=>
[{:type=>
{"id"=>1, "name"=>"electricity"},
:data=>[15, 25, 35, 45]},
{:type=>
{"id"=>2, "name"=>"water"},
:data=>[25, 35, 45, 55]}],
:monthes_data=>
{:monthes=>
["jan", "feb"]}},
{:report_data=>
[{:type=>
{"id"=>1, "name"=>"electricity"},
:data=>[17, 27, 37, 47]},
{:type=>
{"id"=>2, "name"=>"water"},
:data=>[27, 37, 47, 57]}],
:monthes_data=>
{:monthes=>
["jan", "feb"]}}]
acc = {}
arr.each do
|e| e[:report_data].each_with_index do
|e, idx|
type = e[:type]['id']
e[:data].each_with_index do
|e, idx|
acc[type] = [] if not acc[type]
acc[type][idx] = (acc[type][idx] or 0) + e
end
end
end
p acc
outputs
{1=>[42, 72, 102, 132], 2=>[72, 102, 132, 162]}
You should be able to reformat this into your record
Code
def convert(arr)
{ :months_data=>arr.first[:months_data],
:report_data=>arr.map { |h| h[:report_data] }.
transpose.
map { |d| { :type=>d.first[:type] }.
merge(:data=>d.map { |g| g[:data] }.transpose.map { |a| a.reduce(:+) }) }
}
end
Example
Half the battle in problems such as this one is visualizing the data. It's much clearer, imo, when written like this:
arr = [
{:report_data=>[
{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[10, 20, 30, 40]},
{:type=>{"id"=>2, "name"=>"water"}, :data=>[20, 30, 40, 50]}
],
:months_data=>{:months=>["jan", "feb"]}
},
{:report_data=>[
{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[15, 25, 35, 45]},
{:type=>{"id"=>2, "name"=>"water"}, :data=>[25, 35, 45, 55]}
],
:months_data=>{:months=>["jan", "feb"]}
},
{:report_data=>[
{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[17, 27, 37, 47]},
{:type=>{"id"=>2, "name"=>"water"}, :data=>[27, 37, 47, 57]}],
:months_data=>{:months=>["jan", "feb"]}
}
]
Let's try it:
convert(arr)
#=> {:months_data=>{:months=>["jan", "feb"]},
# :report_data=>[
# {:type=>{"id"=>1, "name"=>"electricity"}, :data=>[42, 72, 102, 132]},
# {:type=>{"id"=>2, "name"=>"water"}, :data=>[72, 102, 132, 162]}
# ]
# }
Explanation
The first thing I did is concentrate on computing the sums, so I converted this to the values of :report_data. That key, and the key-value pair of months' data, which is the same for all elements (hashes) of arr, can be added back in later.
b = arr.map { |h| h[:report_data] }
#=> [
# [{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[10, 20, 30, 40]},
# {:type=>{"id"=>2, "name"=>"water"}, :data=>[20, 30, 40, 50]}
# ],
# [{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[15, 25, 35, 45]},
# {:type=>{"id"=>2, "name"=>"water"}, :data=>[25, 35, 45, 55]}
# ],
# [{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[17, 27, 37, 47]},
# {:type=>{"id"=>2, "name"=>"water"}, :data=>[27, 37, 47, 57]}
# ]
# ]
If you are not certain that the elements of each array will be sorted by "id", you could write:
b = arr.map { |h| h[:report_data].sort_by { |g| g[:type]["id"] } }
c = b.transpose
#=> [
# [{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[10, 20, 30, 40]},
# {:type=>{"id"=>1, "name"=>"electricity"}, :data=>[15, 25, 35, 45]},
# {:type=>{"id"=>1, "name"=>"electricity"}, :data=>[17, 27, 37, 47]}
# ],
# [{:type=>{"id"=>2, "name"=>"water"}, :data=>[20, 30, 40, 50]},
# {:type=>{"id"=>2, "name"=>"water"}, :data=>[25, 35, 45, 55]},
# {:type=>{"id"=>2, "name"=>"water"}, :data=>[27, 37, 47, 57]}
# ]
# ]
e = c.map {|d| { :type=>d.first[:type] }.
merge(:data=>d.map { |g| g[:data] }.transpose.map { |a| a.reduce(:+) }) }
#=> [{:type=>{"id"=>1, "name"=>"electricity"}, :data=>[42, 72, 102, 132]},
# {:type=>{"id"=>2, "name"=>"water"} , :data=>[72, 102, 132, 162]}]
Lastly, we need to put the put the key :report_data back in and add the months' data:
{ :months_data=>arr.first[:months_data], :report_data=>e }
#=> {:months_data=>{:months=>["jan", "feb"]},
# :report_data=>[
# {:type=>{"id"=>1, "name"=>"electricity"}, :data=>[42, 72, 102, 132]},
# {:type=>{"id"=>2, "name"=>"water"}, :data=>[72, 102, 132, 162]}
# ]
# }
For clarity I've reformatted the input array and removed the :monthes_data key, since that seems to be unrelated to your question. Here's our data:
TL;DR
def zip_sum(arr1, arr2)
return arr2 if arr1.nil?
arr1.zip(arr2).map {|a, b| a + b }
end
def sum_report_data(arr)
arr.flat_map do |item|
item[:report_data].map {|datum| datum.values_at(:type, :data) }
end
.reduce({}) do |sums, (type, data)|
sums.merge(type => data) do |_, old_data, new_data|
zip_sum(old_data, new_data)
end
end
.map {|type, data| { type: type, data: data } }
end
p sum_report_data(arr)
# =>
[ { type: { "id" => 1, "name" => "electricity" }, data: [ 42, 72, 102, 132 ] },
{ type: { "id" => 2, "name" => "water" }, data: [ 72, 102, 132, 162 ] }
]
Explanation
arr = [
{ report_data: [
{ type: { "id" => 1, "name" => "electricity" },
data: [ 10, 20, 30, 40 ]
},
{ type: { "id" => 2, "name" => "water" },
data: [ 20, 30, 40, 50 ]
}
]
},
{ report_data: [
{ type: { "id" => 1, "name" => "electricity" },
data: [ 15, 25, 35, 45 ]
},
{ type: { "id" => 2, "name" => "water" },
data: [ 25, 35, 45, 55 ]
}
]
},
{ report_data: [
{ type: { "id" => 1, "name" => "electricity" },
data: [ 17, 27, 37, 47 ]
},
{ type: { "id" => 2, "name" => "water" },
data: [ 27, 37, 47, 57 ]
}
]
}
]
Step 1
First, let's define a helper method to sum the values of two arrays:
def zip_sum(arr1, arr2)
return arr2 if arr1.nil?
arr1.zip(arr2).map {|a, b| a + b }
end
zip_sum([ 1, 2, 3 ], [ 10, 20, 30 ])
# => [ 11, 22, 33 ]
zip_sum(nil, [ 5, 6, 7 ])
# => [ 5, 6, 7 ]
The way zip_sum works is by "zipping" the two arrays together using Enumerable#zip (e.g. [1, 2].zip([10, 20]) returns [ [1, 10], [2, 20] ]), then adding each pair together.
Step 2
Next, let's use Enumerable#flat_map to get the parts of the data we care about:
result1 = arr.flat_map do |item|
item[:report_data].map {|datum| datum.values_at(:type, :data) }
end
# result1 =>
[ [ { "id" => 1, "name" => "electricity" }, [ 10, 20, 30, 40 ] ],
[ { "id" => 2, "name" => "water" }, [ 20, 30, 40, 50 ] ],
[ { "id" => 1, "name" => "electricity" }, [ 15, 25, 35, 45 ] ],
[ { "id" => 2, "name" => "water" }, [ 25, 35, 45, 55 ] ],
[ { "id" => 1, "name" => "electricity" }, [ 17, 27, 37, 47 ] ],
[ { "id" => 2, "name" => "water" }, [ 27, 37, 47, 57 ] ]
]
Above we've just grabbed the :type and :data values out of each hash the :report_data arrays.
Step 3
Next let's use Enumerable#reduce to iterate over the array of arrays and calculate a running sum of the :data values using the zip_sum method we defined earlier:
result2 = result1.reduce({}) do |sums, (type, data)|
sums.merge(type => data) do |_, old_data, new_data|
zip_sum(old_data, new_data)
end
end
# result2 =>
{ { "id" => 1, "name" => "electricity" } => [ 42, 72, 102, 132 ],
{ "id" => 2, "name" => "water" } => [ 72, 102, 132, 162 ]
}
The result might look a little odd to you because we usually use strings or symbols as hash keys, but in this hash we're using other hashes (the :type values from above) as keys. That's one nice thing about Ruby: You can use any object as a key in a hash.
Inside the reduce block, sums is the hash that's ultimately returned. It starts out as an empty hash ({}, the value we passed to reduce as an argument). type is the hash we're using as a key and data is the array of integers. In each iteration the next values from the result2 array are assigned to type, but sums is updated with whatever value was returned at the end of the block in the previous iteration.
We're using Hash#merge in kind of a tricky way:
sums.merge(type => data) do |_, old_data, new_data|
zip_sum(old_data, new_data)
end
This merges the hash { type => data } (remember that type is the :type hash
and data is the array of integers) into the hash sums. If there are any key collisions, the block will be invoked. Since we only have one key, type, then the block will be invoked if sums[type] already exists. If it does, we call zip_sum with the previous value of sums[type] and data, effectively keeping a running sum of data.
In effect, it's basically doing this:
sums = {}
type, data = result2[0]
sums[type] = zip_sum(sums[type], data)
type, data = result2[1]
sums[type] = zip_sum(sums[type], data)
type, data = result2[3]
# ...and so on.
Step 4
We now have this hash in result3:
{ { "id" => 1, "name" => "electricity" } => [ 42, 72, 102, 132 ],
{ "id" => 2, "name" => "water" } => [ 72, 102, 132, 162 ]
}
That's the data we want, so now we just have to take it out of this weird format and put it into a regular hash with the keys :type and :data:
result3 = result2.map {|type, data| { type: type, data: data } }
# result3 =>
[ { type: { "id" => 1, "name" => "electricity" },
data: [ 42, 72, 102, 132 ]
},
{ type: { "id" => 2, "name" => "water" },
data: [ 72, 102, 132, 162 ]
}
]