我有两个散列数组,它们由一组共同的键关联:
数组1为:
[
{0=>"pmet-add-install-module-timings.patch"},
{1=>"pmet-change-sample-data-load-order.patch"},
{2=>"pmet-configurable-recurring.patch"},
{3=>"pmet-consumers-run-staggered-by-sleep.patch"},
{4=>"pmet-dynamic-block-segment-display.patch"},
{5=>"pmet-fix-admin-label-word-breaking.patch"},
{6=>"pmet-fix-invalid-module-dependencies.patch"},
{7=>"pmet-fix-invalid-sample-data-module-dependencies.patch"},
{8=>"pmet-fix-module-loader-algorithm.patch"},
{9=>"pmet-fix-sample-data-code-generator.patch"},
{10=>"pmet-remove-id-requirement-from-layout-update-file.patch"},
{11=>"pmet-specify-store-id-for-order.patch"},
{12=>"pmet-staging-preview-js-fix.patch"},
{13=>"pmet-stop-catching-sample-data-errrors-during-install.patch"},
{14=>"pmet-visitor-segment.patch"}
]
数组2为:
[
{0=>"magento2-base"},
{1=>"magento/module-sample-data"},
{2=>"magento/module-configurable-sample-data"},
{3=>"magento/module-message-queue"},
{4=>"magento/module-banner"},
{5=>"magento/theme-adminhtml-backend"},
{6=>"magento/module-staging"},
{7=>"magento/module-gift-registry-sample-data"},
{8=>"magento2-base"},
{9=>"magento/module-downloadable-sample-data"},
{10=>"magento/module-catalog"},
{11=>"magento/module-sales-sample-data"},
{12=>"magento/module-staging"},
{13=>"magento2-base"},
{14=>"magento/module-customer"}
]
这些数组中的哈希具有相同的索引集,并且第二个数组在键0
,8
和13
以及6
和12
中具有重复的值。
我的目标是将来自这两个数据集的值缝合在一起,成为一组嵌套的哈希。数组2中任何有重复值的地方,我都需要从数组1中收集其关联值,并将它们包括在嵌套哈希中。
例如,从数组2获得magento2-base
值,从数组1获得与键相关的值。Ruby中的哈希结构如下:
hash = {
"magento2-base" => [
{0 => "m2-hotfixes/pmet-add-install-module-timings.patch"},
{8 => "m2-hotfixes/pmet-fix-module-loader-algorithm.patch"},
{13 => "m2-hotfixes/pmet-stop-catching-sample-data-errrors-during-install.patch"}
]
}
对于来自数组2的任何其他重复值也是如此,例如,magento/module-staging
将是:
hash = {
"magento/module-staging" => [
{6 => "pmet-fix-invalid-module-dependencies.patch"},
{12 => "pmet-staging-preview-js-fix.patch"}
]
}
将这些需求结合在一起的结果哈希的较大摘录如下:
hash = {
"magento2-base" =>
[
{0 => "m2-hotfixes/pmet-add-install-module-timings.patch"},
{8 => "m2-hotfixes/pmet-fix-module-loader-algorithm.patch"},
{13 => "m2-hotfixes/pmet-stop-catching-sample-data-errrors-during-install.patch"}
],
"magento/module-sample-data" =>
{0 => "pmet-change-sample-data-load-order.patch"},
"magento/module-configurable-sample-data" =>
{2 => "pmet-configurable-recurring.patch"},
"magento/module-message-queue" =>
{3 => "pmet-consumers-run-staggered-by-sleep.patch"}
"magento/module-staging" =>
[
{6 => "pmet-fix-invalid-module-dependencies.patch"},
{12 => "pmet-staging-preview-js-fix.patch"}
],
...
}
我使用了一个嵌套循环,该循环将两个数组组合在一起以链接键,并尝试从数组2中提取重复项,并认为我需要同时维护数组2和数组2中重复值的数组。数组1中与它们关联的值的数组。然后,我将使用一些数组合并魔术将它们重新组合在一起。
这是我所拥有的:
found_modules_array = []
duplicate_modules_array = []
duplicate_module_hash = {}
file_collection_array = []
modules_array.each do |module_hash|
module_hash.each do |module_hash_key, module_hash_value|
files_array.each do |file_hash|
file_hash.each do |file_hash_key, file_hash_value|
if module_hash_key == file_hash_key
if found_modules_array.include?(module_hash_value)
duplicate_module_hash = {
module_hash_key => module_hash_value
}
duplicate_modules_array << duplicate_module_hash
end
found_modules_array << module_hash_value
end
end
end
end
end
在此代码中,files_array
是数组1,modules_array
是数组2。found_modules_array
是存储所有重复项的存储区,然后将它们推入duplicate_module_hash
,然后将其推入duplicates_modules_array
。] >
此解决方案:
以下数据中详细解释了上述数据结构的路径:Using array values as hash keys to create nested hashes in Ruby
我将在下面进行总结:
我有一个文件目录。其中大多数是.patch
文件,尽管有些不是。对于每个补丁文件,我需要扫描始终为字符串的第一行,并提取该行的一部分。结合每个文件的名称,每个第一行的该部分以及每个文件的唯一标识符,我需要创建一个哈希,然后将其转换为json并写入文件。
以下是示例:
文件目录:
|__ .gitkeep |__ pmet-add-install-module-timings.patch |__ pmet-change-sample-data-load-order.patch
第一行示例:
File Name: `pmet-add-install-module-timings.patch` First Line: `diff --git a/setup/src/Magento/Setup/Model/Installer.php b/setup/src/Magento/Setup/Model/Installer.php` File Name: `pmet-change-sample-data-load-order.patch` First Line: `diff --git a/vendor/magento/module-sample-data/etc/module.xml b/vendor/magento/module-sample-data/etc/module.xml` File Name: `pmet-stop-catching-sample-data-errrors-during-install.patch` First Line: `diff --git a/vendor/magento/framework/Setup/SampleData/Executor.php b/vendor/magento/framework/Setup/SampleData/Executor.php` File Name: `pmet-fix-admin-label-word-breaking.patch` First Line: `diff --git a/vendor/magento/theme-adminhtml-backend/web/css/styles-old.less b/vendor/magento/theme-adminhtml-backend/web/css/styles-old.less`
示例Json文件:
{ "patches": { "magento/magento2-base": { "Patch 1": "m2-hotfixes/pmet-add-install-module-timings.patch" }, "magento/module-sample-data": { "Patch 2": "m2-hotfixes/pmet-change-sample-data-load-order.patch" }, "magento/theme-adminhtml-backend": { "Patch 3": "m2-hotfixes/pmet-fix-admin-label-word-breaking.patch" }, "magento/framework": { "Patch 4": "m2-hotfixes/pmet-stop-catching-sample-data-errrors-during-install.patch" } } }
我遇到的问题是,尽管json允许重复的键,但ruby哈希不允许,所以从json文件中删除了项目,因为它们是从哈希中删除的。为了解决这个问题,我假设我需要创建指定的数组结构,以便将ID保持为所抓取的文件与属于它们的对应数据之间的一致标识符,以便可以将数据以不同的方式放在一起。现在我意识到情况并非如此,因此我将方法切换为使用以下方法:
files.each_with_index do |file, key| value = File.open(file, &:readline).split('/')[3] if value.match(/module-/) || value.match(/theme-/) result = "magento/#{value}" else result = "magento2-base" end file_array << file module_array << result end
这将产生下面建议的扁平哈希。
我有两个由一组共同的键关联的哈希数组:数组1为:[{0 =>“ pmet-add-install-module-timings.patch”},{1 =>“ pmet-change -sample-data-load-order.patch“},{2 => ...
让arr1
和arr2
为您的两个数组。由于它们的大小相同,并且对于每个索引i
,arr1[i][i]
和arr2[i][i]
是散列i
和arr1[i]
的键arr2[i]
的值,因此可以得到所需的结果很容易获得:
首先,结构