I have a nested array in puppet
$rules = [ [service1, rule1], [service1, rule2], [service2, rule3], [service3, rule4] ]
Which I need to convert to the following hash
$rules_hash = {
'service1' => ['rule1', 'rule2'],
'service2' => ['rule3'],
'service3' => ['rule4'],
}
I tried using the built-in Hash() function but it is not an option since when there are duplicate keys it returns only the last instance of that key, leaving me with an incomplete hash of only one value per key.
I have also tried using reduce() to iterate the array and create a string "rule1 rule2" for the values with a common key, however using reduce() the nested array gets flattened and it becomes impossible to differentiate between the key and value of the hash I want to create without specifically declaring what element of the array should be a key (which becomes unfeasible if the array has a lot of different keys).