如何创建这个multidemsional数组,散列组合? [英] How do I create this multidemsional Array, Hash Combo?
本文介绍了如何创建这个multidemsional数组,散列组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个数据,我需要这样类型的输出的输出。我基本上需要都场地和的日期和与之关联的....如果有人能想到更好的结构,以及如何实现它,我会很感激的所有歌曲...
{
[矢量竞技场 - 奥克兰,新西兰=>
{
2010-10-10=> [进入Sandman,不可饶恕,和正义]
}],
[布里斯班娱乐中心 - 布里斯班昆士兰,Austr ......=>
{
2010-10-11=> [进入Sandman]
}]
}
到目前为止,我想这...不知道如果我在正确的方向移动我..
@venues = Hash.new {|哈希键|哈希[关键] = 0}
@requests = Request.find_all_by_artist(Metallica的)
@ requests.each做|请求|
@venues [request.venue] = request.showdate
结束
解决方案
下面是一个有效的解决方案:
结果= Hash.new {| H1,K1 | H1 [K] = {Hash.new | H2,K2 | H2 [K2] = []}}
Request.find_all_by_artist(Metallica的
:选择=> DISTINCT会场,showdate相似,LOWER(歌曲)AS歌
)。每个做| REQ |
结果[req.venue] [req.showdate<< req.song.titlecase
结束
I have this data and i need an output like this type of output.. I basically need to have all the venues and their dates and all the songs associated to them ....if anyone can think of a better structure and how to achieve it I would be very thankful...
{
["Vector Arena - Auckland Central, New Zealand" =>
{
"2010-10-10" => ["Enter Sandman", "Unforgiven", "And justice for all"]
}],
["Brisbane Entertainment Centre - Brisbane Qld, Austr..." =>
{
"2010-10-11" => ["Enter Sandman"]
}]
}
so far i tried this...and not sure if i am moving in the right direction..
@venues = Hash.new {|hash, key| hash[key] = 0}
@requests = Request.find_all_by_artist("Metallica")
@requests.each do |request|
@venues[request.venue] = request.showdate
end
解决方案
Here is an efficient solution:
result = Hash.new {|h1, k1| h1[k1] = Hash.new{|h2, k2| h2[k2] = []}}
Request.find_all_by_artist("Metallica",
:select => "DISTINCT venue, showdate, LOWER(song) AS song"
).each do |req|
result[req.venue][req.showdate] << req.song.titlecase
end
这篇关于如何创建这个multidemsional数组,散列组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文