如何创建这个多重数组、哈希组合?

发布于 2024-09-26 08:09:26 字数 701 浏览 3 评论 0原文

我有 此数据,我需要类似这种类型的输出。我基本上需要拥有所有场地和他们的日期以及与他们相关的所有歌曲...如果有人能想到更好的结构以及如何实现它,我将非常感激...

{ 
["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"]
    }]
 }

到目前为止我尝试过这个...并且不确定我是否要搬进来正确的方向..

@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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望喜 2024-10-03 08:09:26

这是一个有效的解决方案:

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

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
阳光下的泡沫是彩色的 2024-10-03 08:09:26

我认为你的结构不太正确。它应该像

[ 
{"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"]
    }}
 ]

尝试这个代码

@venues = []
      all_venues = Request.find(:all, :select => "distinct venue, showdate")
      all_venues.each do |unique_venue|
        venue_hash = {}
        showdate_hash = {}
        song_lists = []

        requests = Request.find_all_by_venue(unique_venue.venue)  
        requests.each do |each_request|
          song_lists << each_request.song
        end
        showdate_hash[unique_venue.showdate] = song_lists

        venue_hash[unique_venue.venue] = showdate_hash
        @venues << venue_hash
      end

希望你至少明白这个想法。

I think you structure is not quite right. It should be like

[ 
{"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"]
    }}
 ]

Try this code

@venues = []
      all_venues = Request.find(:all, :select => "distinct venue, showdate")
      all_venues.each do |unique_venue|
        venue_hash = {}
        showdate_hash = {}
        song_lists = []

        requests = Request.find_all_by_venue(unique_venue.venue)  
        requests.each do |each_request|
          song_lists << each_request.song
        end
        showdate_hash[unique_venue.showdate] = song_lists

        venue_hash[unique_venue.venue] = showdate_hash
        @venues << venue_hash
      end

Hope you get the idea at least.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文