Skip to content

Testing whether an Hash is empty without extra functions from modules (like stdlib). Puppet 4.10 #5

Answered by pier4r
pier4r asked this question in Q&A
Discussion options

You must be logged in to vote

Pierfrancesco

I was thinking, could it be that
if $fact['xxx'] != {}
is true also if the fact is undef ? (same for if (! $fact['xxx'] == {} )
and thus Hash[Scalar, Data, 1] is safer?
New

helindbe

the first is false if, and only if the value is a hash and is empty, and true for all other values. (edited)
the second is the same

What you want to use depends on what you really want to test for. The empty() function is true for empty array, hash, string, and for undef. If you are interested more strictly in an empty array or hash, you can use =~ Collection[0,0] (empty) or =~ Collection[1] (at least one - i.e. not empty).
You can also use Hash[0,0] for empty hash.

If you want to also ignore has…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@hlindberg
Comment options

@hlindberg
Comment options

Answer selected by pier4r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants