So it seems that I fell for contains function definition, since:
yq -n '["hyper"]|contains(["pe"])'
‘surprisingly’ returns true. I don’t know what usecase this have, still new to funcitolan world but it’s clearly shown in docs as:
jq 'contains(["baz", "bar"])'
Input ["foobar", "foobaz", "blarp"]
Output true
So how to trivially write exact matching, string-is-contained-in-array-contains?
yq -n '["hyper"] as $i | $i - ["pe"] != $i'
? Or what would be correct solution?
2
Answers
Please specify which implementation of
yq
you are using. As you are referring to examples for thecontains
filter in the manual to stedolan/jq, I assume you are using kislyuk/yq, which is a YAML-wrapper around jq.jq’s
contains
tests if its argument is partially contained in the input. As this is done recursively, it also affects the string items of your array, not only the surrounding array itself. And as"pe"
is contained in"hyper"
, it evaluates totrue
.jq offers the
any
filter to test if in a given stream at least one item evaluates totrue
for a given filter. For that filter you could use the equals==
operator which evaluates totrue
for exact matches, and for the stream, if omitted,any
will default to the items of an input array.Just in case, if you do use the other implementation mikefarah/yq, it offers a similar function called
any_c
(as it is not based on jq and hence has a (sometimes) different syntax):Assuming a jq-like implementation, you’d use
IN
(capitalized)The argument to
IN()
is a stream of elements, not an array.