我有几个数组(我们称之为“原始数组”)。每个数组都包含哈希值,在每个哈希值中我都有来自收到的电子邮件的数据。例如,电子邮件地址,姓名等。我还有一个uid,它是收到的电子邮件中的唯一标识符。原始数组之间会有很多重复,数组越多越好(在完美的世界中,它们应该包含相同的电子邮件和相同的电子邮件数据)。
输入样本:
[[{:from_address=>"one@example.com",
:to=>"one@example.com",
:subject=>"Some subject regarding order 12198",
:datetime=>Sat, 27 Jul 2013 08:48:44 +0000,
:uid=>15065,
:extraction_strategy=>1,
:result=>{:order_id=>"12198", :mail_address=>nil, :name=>"Dr. Evil"}},
{:from_address=>"one@example.com",
:to=>"one@example.com",
:subject=>"Some subject regarding order 12199",
:datetime=>Sat, 27 Jul 2013 08:48:48 +0000,
:uid=>15066,
:extraction_strategy=>1,
:result=>{:order_id=>"12199", :mail_address=>nil, :name=>nil}}],
[{:from_address=>"one@example.com",
:to=>"one@example.com",
:subject=>"Some subject regarding order 12197",
:datetime=>Sat, 27 Jul 2013 08:22:48 +0000,
:uid=>15064,
:extraction_strategy=>2,
:result=>{:order_id=>"12197", :mail_address=>"three@example.com", :name=>"Batman"}},
{:from_address=>"one@example.com",
:to=>"one@example.com",
:subject=>"Some subject regarding order 12199",
:datetime=>Sat, 27 Jul 2013 08:48:48 +0000,
:uid=>15066,
:extraction_strategy=>2,
:result=>{:order_id=>"12199", :mail_address=>"two@example.com", :name=>"James Bond"}}]]
我现在想重新排序所有这些,所以我得到一个新的数组(我们称之为“第一级数组”)。在第一级数组中,我想要“第二级数组”,每个数组都包含匹配uid的电子邮件。因此,如果来自其中一个原始阵列的电子邮件与其他原始阵列中的电子邮件具有相同的uid,则应将两封电子邮件放入相同的新二级阵列中。
输出样本:
[[
[{:from_address=>"one@example.com",
:to=>"one@example.com",
:subject=>"Some subject regarding order 12197",
:datetime=>Sat, 27 Jul 2013 08:22:48 +0000,
:uid=>15064,
:extraction_strategy=>2,
:result=>{:order_id=>"12197", :mail_address=>"three@example.com", :name=>"Batman"}}],
[{:from_address=>"one@example.com",
:to=>"one@example.com",
:subject=>"Some subject regarding order 12198",
:datetime=>Sat, 27 Jul 2013 08:48:44 +0000,
:uid=>15065,
:extraction_strategy=>1,
:result=>{:order_id=>"12198", :mail_address=>nil, :name=>"Dr. Evil"}}],
[{:from_address=>"one@example.com",
:to=>"one@example.com",
:subject=>"Some subject regarding order 12199",
:datetime=>Sat, 27 Jul 2013 08:48:48 +0000,
:uid=>15066,
:extraction_strategy=>1,
:result=>{:order_id=>"12199", :mail_address=>"two@example.com", :name=>"James Bond"}},
{:from_address=>"one@example.com",
:to=>"one@example.com",
:subject=>"Some subject regarding order 12199",
:datetime=>Sat, 27 Jul 2013 08:48:48 +0000,
:uid=>15066,
:extraction_strategy=>2,
:result=>{:order_id=>"12199", :mail_address=>nil, :name=>nil}}]
]]
我只能提出需要大量循环和重复的解决方案,但由于数组可能变得非常庞大,我需要一个有效的简洁例程。任何人都可以帮助我吗?
答案 0 :(得分:1)
好吧,两个嵌套循环和一个地图......
a = [[{:from_address=>"one@example.com",
:to=>"one@example.com",
:subject=>"Some subject regarding order 12198",
:datetime=>"Sat, 27 Jul 2013 08:48:44 +0000",
:uid=>15065,
:extraction_strategy=>1,
:result=>{:order_id=>"12198", :mail_address=>nil, :name=>"Dr. Evil"}},
{:from_address=>"one@example.com",
:to=>"one@example.com",
:subject=>"Some subject regarding order 12199",
:datetime=>"Sat, 27 Jul 2013 08:48:48 +0000",
:uid=>15066,
:extraction_strategy=>1,
:result=>{:order_id=>"12199", :mail_address=>nil, :name=>nil}}],
[{:from_address=>"one@example.com",
:to=>"one@example.com",
:subject=>"Some subject regarding order 12197",
:datetime=>"Sat, 27 Jul 2013 08:22:48 +0000",
:uid=>15064,
:extraction_strategy=>2,
:result=>{:order_id=>"12197", :mail_address=>"three@example.com", :name=>"Batman"}},
{:from_address=>"one@example.com",
:to=>"one@example.com",
:subject=>"Some subject regarding order 12199",
:datetime=>"Sat, 27 Jul 2013 08:48:48 +0000",
:uid=>15066,
:extraction_strategy=>2,
:result=>{:order_id=>"12199", :mail_address=>"two@example.com", :name=>"James Bond"}}]]
result = Hash.new {|h,k| h[k] = [] }
a.each { |b| b.each { |h| result[h[:uid]] << h } }
result = result.map { |k, v| v }
...但请注意,要使其工作,我必须将日期时间字段更改为字符串。比我更聪明的人可能能够弄清楚如何解决这个问题。