Ryan Litwiller Ryan Litwiller - 6 months ago 17
Ruby Question

Constructing multi demensional array Ruby

Working with Dashin.io framework to create some nice graphs. I'm trying to construct a specific array output that the application is looking for and I'm very close, but think I need help creating another dimension of the array.

This is what I have:

r = Redis.new(:host => "127.0.0.1", :password => 'password')
assGroups=['group1','group2', 'group3', 'group4']
points = ['Day', 'group1','group2', 'group3', 'group4']

history = 3
i = 0

while i <= history do
getdate = (Date.today-i).strftime("%Y-%m-%d")
i += 1
countArr=[getdate]
points.push(countArr)

for group in assGroups
query = [getdate,":",group].join()
queue = r.hgetall query
dCount = queue['incidents'].to_i
countArr << dCount
end
end
print points


This is giving me

["Day", "group1","group2", "group3", "group4", ["2016-06-06", 63, 13, 86, 13], ["2016-06-05", 64, 13, 84, 13], ["2016-06-04", 63, 13, 84, 13], ["2016-06-03", 64, 13, 84, 13]]


This is very close but I need the, "Day" and groups its own dimension of the array as well, so this would be my desired output:

[["Day", "group1","group2", "group3", "group4"], ["2016-06-06", 63, 13, 86, 13], ["2016-06-05", 64, 13, 84, 13], ["2016-06-04", 63, 13, 84, 13], ["2016-06-03", 64, 13, 84, 13]]

Answer

Looks like the simplest change would be to ensure points is a multidimensional array when you first define it: points = [['Day', 'group1','group2', 'group3', 'group4']]