2016-06-13 175 views
-1

爲Jekyll寫一個插件,因爲我還不熟悉 如何做.select,.map和他們所有的朋友關於數組除了簡單的一個陣列的情況下)從一個陣列返回元素,基於另一個陣列的值

所以我現在的問題是,有此作爲文檔數組,與他們相關的sha1sum代碼:

[ 
    [ 
     #<Jekyll::Document _projects/1st-project.md collection=projects>, 
     "8f918a3d8263a957c206a9864d3507aaa5277a79" 
    ], 
    [ 
     #<Jekyll::Document _posts/2nd-project.markdown collection=posts>, 
     "ca81eda5d49100bdf23db16fe1c9d17040fd33f8" 
    ], 
    [ 
     #<Jekyll::Document _posts/3rd-project.markdown collection=posts>, 
     "37bf18464b00c9a808436853bc6d868aff218eb6" 
    ], 
    ... 
    ... 
] 

而在另一邊,連鎖羣的像散所以:

{ 
    "linkage_groups"=> 
    [ 
     { 
      "group_name"=>"test1", 
      "sha1_sums"=> [ 
       "ca81eda5d49100bdf23db16fe1c9d17040fd33f8", 
       "37bf18464b00c9a808436853bc6d868aff218eb6" 
      ] 
     }, 
     { 
      "group_name"=>"test1", 
      "sha1_sums"=> [ 
       "154c255d59e6063fc609ade2254dc1b09d1de8ab", 
       "3e9ef9f059786888e39df608d104263cf0fdae76" 
      ] 
     } 
    ] 
} 

如何將能夠通過循環這些基團一次一個,並從上述 陣列,其中一個文件,sha1_sum是存在於特定組返回的每個組的sha1_sums文檔。

預期輸出將是散列對於每個組的陣列,保持 文檔其中滿足條件,對他們的sha1_sum存在的基團中:

例第二和第三個項目符合條件,因爲他們sha's是一個名爲test1

[ 
    { 
     "test1" => [#<Jekyll::Document _posts/2nd-project.markdown collection=posts>, #<Jekyll::Document _posts/3rd-project.markdown collection=posts] 
    }, 
    { 
     "test2" => [..., ...] 
    }, 
    { 
     "test3" => [..., ...] 
    }, 
    ... 
] 

由於從@Lukas Baliak答覆組 -

以下是我在屬於同一兩個散列的情況下,我得到組:

{ 
    "ca81eda5d49100bdf23db16fe1c9d17040fd33f8"=>"test1", 
    "b673be35ad73ab48da23b271ab0dda95ea07c905"=>"test1", 
    "154c255d59e6063fc609ade2254dc1b09d1de8ab"=>"test2", 
    "3e9ef9f059786888e39df608d104263cf0fdae76"=>"test2" 
} 

[ 
    [ 
     #<Jekyll::Document _projects/my-first-project.md collection=projects>, 
     "b673be35ad73ab48da23b271ab0dda95ea07c905" 
    ], 
    [ 
     #<Jekyll::Document _posts/2016-06-05-one-more-post.markdown collection=posts>, 
     "ca81eda5d49100bdf23db16fe1c9d17040fd33f8" 
    ] 
] 

{ 
    "test1"=> [#<Jekyll::Document _posts/2016-06-05-one-more-post.markdown collection=posts>, "ca81eda5d49100bdf23db16fe1c9d17040fd33f8"] 
} 

只列出一個文件,爲什麼? b673be35ad73ab48da23b271ab0dda95ea07c905在哪裏?

+2

向我們展示一些預期的輸出。 –

+0

@LukasBaliak請參閱上面的最新內容。 – branquito

+0

好吧,我編輯我的答案 –

回答

2

我preffer使用簡單的數據結構,所以我

doc = [ 
    [ 
    "#<Jekyll::Document _projects/my-first-project.md collection=projects>", 
    "8f918a3d8263a957c206a9864d3507aaa5277a79" 
    ], 
    [ 
    "#<Jekyll::Document _posts/2016-06-05-one-more-post.markdown collection=posts>", 
    "ca81eda5d49100bdf23db16fe1c9d17040fd33f8" 
    ] 
] 

group_config

group_config = { 
    "linkage_groups" => [ 
    { 
     "group_name" => "test1", 
     "sha1_sums" => [ 
     "ca81eda5d49100bdf23db16fe1c9d17040fd33f8", 
     "b673be35ad73ab48da23b271ab0dda95ea07c905" 
     ] 
    }, 
    { 
     "group_name" => "test2", 
     "sha1_sums" => [ 
     "154c255d59e6063fc609ade2254dc1b09d1de8ab", 
     "8f918a3d8263a957c206a9864d3507aaa5277a79" 
     ] 
    } 
    ] 
} 

遷移 「遷移」 組Hash

DOC到Hash

groups = group_config["linkage_groups"].each_with_object({}) do |h, exp| 
    h["sha1_sums"].each { |sha1| exp[sha1] = h["group_name"] } 
end 

出口組

p groups 

# { 
# "ca81eda5d49100bdf23db16fe1c9d17040fd33f8" => "test1", 
# "b673be35ad73ab48da23b271ab0dda95ea07c905" => "test1", 
# "154c255d59e6063fc609ade2254dc1b09d1de8ab" => "test2", 
# "8f918a3d8263a957c206a9864d3507aaa5277a79" => "test2" 
# } 

而且過程中生成散列結構

export = doc.each_with_object({}) do |arr, exp| 
    exp[groups[arr[1]]] = arr 
end 

輸出

p export 

# { 
# "test2" => ["#<Jekyll::Document _projects/my-first-project.md collection=projects>", "8f918a3d8263a957c206a9864d3507aaa5277a79"], 
# "test1" => ["#<Jekyll::Document _posts/2016-06-05-one-more-post.markdown collection=posts>", "ca81eda5d49100bdf23db16fe1c9d17040fd33f8"] 
# } 

編輯:

如果您需要更多然後一個使用這種修改

export = doc.each_with_object(Hash.new{|k, v| k[v] = []}) do |arr, exp| 
    exp[groups[arr[1]]] << arr 
end 

EDIT 2

好吧,如果你需要一個在SHA1_HASH更多的羣體可能是您可以使用此更新。

groups = group_config["linkage_groups"].each_with_object(Hash.new { |k, v| k[v] = [] }) do |h, exp| 
    h["sha1_sums"].each { |sha1| exp[sha1] << h["group_name"] } 
end 

p groups 

# { 
# "ca81eda5d49100bdf23db16fe1c9d17040fd33f8" => ["test1"], 
# "b673be35ad73ab48da23b271ab0dda95ea07c905" => ["test1"], 
# "8f918a3d8263a957c206a9864d3507aaa5277a79" => ["test1", "test2"], 
# "154c255d59e6063fc609ade2254dc1b09d1de8ab" => ["test2"] 
# } 

過程

export = doc.each_with_object(Hash.new { |k, v| k[v] = [] }) do |arr, exp| 
    groups[arr[1]].each { |group| exp[group] << arr } 
end 

輸出

p export 

# { 
# "test1" => [ 
#  ["#<Jekyll::Document _projects/my-first-project.md collection=projects>", "8f918a3d8263a957c206a9864d3507aaa5277a79"], 
#  ["#<Jekyll::Document _posts/2016-06-05-one-more-post.markdown collection=posts>", "ca81eda5d49100bdf23db16fe1c9d17040fd33f8"] 
# ], 
# "test2" => [ 
#  ["#<Jekyll::Document _projects/my-first-project.md collection=projects>", "8f918a3d8263a957c206a9864d3507aaa5277a79"], 
#  ["#<Jekyll::Document _posts/2016-06-0s-one-more-post.markdown collection=posts>", "154c255d59e6063fc609ade2254dc1b09d1de8ab"] 
# ] 
# } 

我希望這將有助於。

+0

好極了,現在只是把我的頭圍繞這些'each_with_object'事物..沒有得到它。 – branquito

+0

其實我用第一個'{}'得到了,但最後一個用'Hash.new {}'是令人困惑的。 – branquito

+1

@branquito也許這會有所幫助。 http://stackoverflow.com/questions/19064209/how-is-each-with-object-supposed-to-work和http://stackoverflow.com/questions/37679425/need-to-understand-hash-of- hashes-in-ruby/ –

1

你不是很清楚你的問題。有時候有必要詳細說明對於哪個輸入需要輸出什麼。我假設array包含Document/Sha1對,hash包含linkage_groups

hash看起來是這樣的:

{"linkage_groups"=>[{"group_name"=>"test1", "sha1_sums"=>["ca81eda5d49100bdf23db16fe1c9d17040fd33f8", "b673be35ad73ab48da23b271ab0dda95ea07c905"]}, {"group_name"=>"test1", "sha1_sums"=>["154c255d59e6063fc609ade2254dc1b09d1de8ab", "3e9ef9f059786888e39df608d104263cf0fdae76"]}]} 

而且array看起來是這樣的:

[["Document1", "8f918a3d8263a957c206a9864d3507aaa5277a79"], ["Document2", "ca81eda5d49100bdf23db16fe1c9d17040fd33f8"]] 

我想嘗試這樣的事:

hash["linkage_groups"].each { |group| // for each linkage group 
    group["sha1_sums"].each { |sha1| // for each sha1 in group 
     array.each { |array_element| // for each array element (which itself is an array of document/sha1 pairs 
      if array_element.include?(sha1) then 
       puts "#{array_element[0]} found in #{group["group_name"]} with #{group["sha1"]}" 
      end 
     } 
    } 
} 

我離開它給你管理如何根據你的需要返回元素。