Iterate through sorted Hash keys #23
XMol
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Who hasn't run into that use-case? You want to iterate through the keys of a Hash in order and the usual solution to that is
$hash.keys.sort.each |$key| { $value = $hash[$key] }
. This idiom was very common especially for templates with older Puppet versions, where iteration of Hashes wasn't guaranteed to be idempotent. However, I still like to dump Hashes in key-order, so the output is scanable for specific keys. With inspiration from Puppet's owneach()
function, I came up with...Use this function in EPP templates like this:
... just like basic each, though the output will be sorted according to the keys!
Beta Was this translation helpful? Give feedback.
All reactions