2016-06-13 169 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 
     } 
    } 
} 

我离开它给你管理如何根据你的需要返回元素。