Put data in specific header
headers = ["05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22"]
data = ["05:01", "05:23", "05:43", "06:03", "06:33", "06:53", "07:03", "07:23", "07:46", "08:04", "08:24", "08:44", "09:14", "09:24", "09:44", "10:19", "10:39", "11:02", "11:22", "11:42", "12:12", "12:32", "12:52", "13:12", "13:32", "13:52", "14:12", "14:32", "14:42", "15:02", "15:32", "15:52", "16:12", "16:32", "16:52", "17:12", "17:32", "17:52", "18:12", "18:47", "19:23", "19:48", 开发者_开发技巧"20:28", "21:03", "21:33", "22:08", "22:40"]
I must put specific data to correct header. For example
05 06
05:01 06:03
05:23 06:33
05:43 06:53
Rails 3.0.3
Ruby 1.9.2
Prawn 0.8.4
How I can do that ?
Basically i want do something like this.
Prawn::Document.generate(path) do
table([['05','06']] + [['05:01','06:03'],['05:23','06:33'],['05:43','06:53']], :header => true) do
end
end
mapped_headers = headers.map do |header|
data_array = data.select {|d| header == d[0,2]}
[header, data_array]
end
This code will give output like this:
[["05", ["05:01", "05:23", "05:43"]], ["06", ["06:03", "06:33", "06:53"]], ["07", ["07:03", "07:23", "07:46"]], ["08", ["08:04", "08:24", "08:44"]], ["09", ["09:14", "09:24", "09:44"]], ["10", ["10:19", "10:39"]], ["11", ["11:02", "11:22", "11:42"]], ["12", ["12:12", "12:32", "12:52"]], ["13", ["13:12", "13:32", "13:52"]], ["14", ["14:12", "14:32", "14:42"]], ["15", ["15:02", "15:32", "15:52"]], ["16", ["16:12", "16:32", "16:52"]], ["17", ["17:12", "17:32", "17:52"]], ["18", ["18:12", "18:47"]], ["19", ["19:23", "19:48"]], ["20", ["20:28"]], ["21", ["21:03", "21:33"]], ["22", ["22:08", "22:40"]]]
You can also create a hash by editing last line
{header => data_array}
You can acheive Prawn requirement by doing this
rows = 3
datas = (0...rows).map do |row|
mapped_headers.map{|header| header[1][row]}
end
Prawn::Document.generate(path) do
table([headers] + datas, :header => true) do
end
end
If you are asking about how to add a new element to an array, method push
will do the job:
headers.push "05"
data.push "05:01"
精彩评论