2013-07-09 39 views
-1

我有散列值的兩個數組:如何通過哈希鍵值之一合併兩個散列數組?

[{:status=>"failed", :tag=>"tag156", :node=>"isfw-a"}, 
{:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a"}, 
{:status=>"changed", :tag=>"tag156", :node=>"vpfw-a"}] 

[{:status=>"success", :sender=>"ayfw-a"}, 
{:status=>"success", :sender=>"vpfw-a"}] 

我可以用它來合併的關鍵是:node:sender。 我需要從第二陣列:another_status添加:status參數的第一陣列,將「跳過」那裏的第二陣列中沒有對應的:sender,像這樣:

[{:status=>"failed", :tag=>"tag156", :node=>"isfw-a", :another_status=>"skipped"}, 
{:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a", :another_status=>"success"}, 
{:status=>"changed", :tag=>"tag156", :node=>"vpfw-a", :another_status=>"success"}] 

我不能想出解決方案來實現這一點。

回答

2

像這樣的事情吧?

a1 = [ 
    {:status=>"failed", :tag=>"tag156", :node=>"isfw-a"}, 
    {:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a"}, 
    {:status=>"changed", :tag=>"tag156", :node=>"vpfw-a"}, 
] 

a2 = [ 
    {:status=>"success", :sender=>"ayfw-a"}, 
    {:status=>"success", :sender=>"vpfw-a"}, 
] 

sender_status = Hash[ a2.map { |item| [ item[:sender], item[:status] ] } ] 

a1.each do |item| 
    item[:another_status] = sender_status[item[:node]] || 'skipped' 
end 

a1.each { |item| p item } 

輸出

{:status=>"failed", :tag=>"tag156", :node=>"isfw-a", :another_status=>"skipped"} 
{:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a", :another_status=>"success"} 
{:status=>"changed", :tag=>"tag156", :node=>"vpfw-a", :another_status=>"success"} 
-1
a1 = [{:status=>"failed", :tag=>"tag156", :node=>"isfw-a"}, 
{:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a"}, 
{:status=>"changed", :tag=>"tag156", :node=>"vpfw-a"}] 

a2 = [{:status=>"success", :sender=>"ayfw-a"}, 
{:status=>"success", :sender=>"vpfw-a"}] 

a1.map do |h1| h1.merge(another_status: 
    a2.find{|h2| h2[:sender] == h1[:node]}.to_h[:status] || "skipped" 
) end 

輸出

[ 
    { 
    :status   => "failed", 
    :tag   => "tag156", 
    :node   => "isfw-a", 
    :another_status => "skipped" 
    }, 
    { 
    :status   => "unchanged", 
    :tag   => "tag156", 
    :node   => "ayfw-a", 
    :another_status => "success" 
    }, 
    { 
    :status   => "changed", 
    :tag   => "tag156", 
    :node   => "vpfw-a", 
    :another_status => "success" 
    } 
] 
+1

這是一件好事,顯示你的代碼,提示,暗示的結果。 –

-1
ar1 = [ {:status=>"failed", :tag=>"tag156", :node=>"isfw-a"}, 
     {:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a"}, 
     {:status=>"changed", :tag=>"tag156", :node=>"vpfw-a"}] 

ar2 = [{:status=>"success", :sender=>"ayfw-a"}, 
     {:status=>"success", :sender=>"vpfw-a"}] 

new_hsh = ar1.map do |a1| 
    bol = ar2.select{|a2| a2[:sender] == a1[:node] } 
    bol ? a1[:another_status]=bol[:status] : a1[:another_status]= 'skipped' 
    a1 
end 

new_ary 
# => [{:status=>"failed", 
#  :tag=>"tag156", 
#  :node=>"isfw-a", 
#  :another_status=>"skipped"}, 
#  {:status=>"unchanged", 
#  :tag=>"tag156", 
#  :node=>"ayfw-a", 
#  :another_status=>"success"}, 
#  {:status=>"changed", 
#  :tag=>"tag156", 
#  :node=>"vpfw-a", 
#  :another_status=>"success"}] 
+0

需求是從相應的'ar2'哈希的':status'值的'ar1'哈希中填充':another_status'值。相反,它只是將其設置爲「成功」字面值。 – Borodin