https://www.puppet.com/community logo
Title
b

brokencode

05/25/2023, 10:05 AM
I have a hash in format 'cert1' => {}, 'cert2 => {},.. I would like to create a array with hash keys in format [ '/etc/cert/cert1', '/etc/cert/cert2',...]. What function to use to get there?
Found a solution after some googling.
$_certificates = $certificates.keys()
$_watch_files = $_certificates.map |$x| { "/etc/letsencrypt/${x}/cert.pem" }
y

Yury Bushmelev

05/25/2023, 10:19 AM
you can do in one line with
$_watch_files = $certificates.keys().map |$x| { ... }
👍 1
h

helindbe

05/25/2023, 3:04 PM
also, when chaining function calls like that and there are no arguments to pass in a call, the parentheses can be skipped - thus making it:
$_watch_files = $certificates.keys.map |$x| { ... }
n

natemccurdy

05/25/2023, 4:04 PM
And you don't even necessarily need the
keys()
function. Here it is with 1 function call:
$certificates = { 'cert1' => {}, 'cert2' => {} }
$_watch_files = $certificates.map |$k, $_v| { "/etc/letsencrypt/${k}/cert.pem" }
Though the clarity of
keys()
is nice.
👍🏻 1
👍 2