0

我有几个数组(我们称它们为“原始数组”)。每个数组都包含散列,在每个散列中我都有来自收到的电子邮件的数据。例如电子邮件地址、姓名等。我还有一个 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}}]
   ]]

我只能想出需要大量循环和重复的解决方案,但是由于数组可能会变得非常庞大,因此我需要一个有效的简洁例程。谁能帮我吗?

4

1 回答 1

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 }

...但请注意,要使其正常工作,我必须将日期时间字段更改为字符串。比我聪明的人可能会想办法解决这个问题。

于 2013-07-29T02:13:34.110 回答