Ask Your Question

Verifying existance of keys in hash

asked 2014-08-21 12:10:57 -0500

Paolo gravatar image

We retrieve a hash with hiera. Said hash should have 3 mandatory keys and 2 optional keys (unfortunately users are hard to teach) so to avoid problems due to partial hashes how do I validate that the retrieved hash has the 3 mandatory keys in it?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-08-22 07:50:11 -0500

llowder gravatar image

You'll want to use the validare_hash() and has_key() functions from the stdlib module.

$my_hash = {'key_one' => 'value_one'}


if has_key($my_hash, 'key_two') {
  notice('we will not reach here')
if has_key($my_hash, 'key_one') {
  notice('this will be printed')

If you don't want to abort catalog compilation, you can use is_hash() and an if block.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-08-21 12:10:57 -0500

Seen: 5,808 times

Last updated: Aug 22 '14