A list of "Contains" for "OR"

Hi all,

How can I avoid repeating “computer name contains” in the query:

if computer name contains “abc” OR computer name contains “def” OR computer name contains “ghi” then “Do A” else if computer name contains “jkl” OR computer name contains “mno” OR computer name contains “pqr” then “Do B” else if computer name contains “stu” OR computer name contains “vwx” OR computer name contains “yz” then “Do C” else nothing

I just know it’s going to be something super simple but I can’t find the right keywords for my searches…

Thanks!

As usual, there are many different ways to express one thought. Some of it depends on readability vs brevity. Here are a few ways that should work (though I’ve not tested)

(if (it contains "abc" OR it contains "def" OR it contains "ghi") then “Do A” else if (it contains "jkl" OR it contains “mno” OR it contains "pqr") then "Do B" else if (it contains "stu" OR it contains "vwx" OR it contains "yz") then "Do C" else nothing) of computer name as lowercase

If these are exclusive (a computer name will not contain both “abc” and “xyz”), you could use an iterator for plurals rather than if/then/else

items 1 of ((("abc";"def"), "first thing");("def";"ghi"), "second")) whose (computer name as lowercase contains item 0 of it)

1 Like

Exactly what does an iterator do; and what’s the correlation between 0 and 1 here?
(They are indeed exclusive.)

Iterators are a new concept for me, thank you for the introduction :slight_smile:

An iterator is not necessarily a BigFix term, but just refers to looping through a set of data (“iterating” it) and performing some action on each element of the data.

It sounds like you have an interest in the Relevance language, so I’d highly recommend starting with the Tutorial at https://developer.bigfix.com/relevance/tutorial.html and then moving up through the rest of the inspector guides linked at https://developer.bigfix.com/relevance/guide/

For this case, we can look at a couple of things. “Tuples” and “Plurals” are one of the more confusing distinctions of beginner relevance, so for you (or anyone else reading in the future), I’ll try to put together a primer here; but really it just takes practice to make it click and the tutorials really are helpful there.

Consider a “tuple”, commonly used in Relevance, Perl, Python, and some other languages heavy on text-parsing. A “tuple” (think “multiple”) is similar to an array; elements are separated by a comma and they can be referred to individually as “item 0 of it” or “item 1 of it”, for example:

q: ("one", "two", "three")
A: one, two, three
T: 0.043 ms
I: singular ( string, string, string )

q: item 0 of ("one", "two", "three")
A: one
T: 0.025 ms
I: singular string

q: (item 1 of it, item 2 of it) of ("one", "two", "three")
A: two, three
T: 0.068 ms
I: singular ( string, string )

This is different from a Plural. A Plural is an unordered collection of answers. We can create plurals with an inspector like files of folder "X", or artificially create them by separating elements with a semicolon.

q: ("one"; "two"; "three")
A: one
A: two
A: three
T: 0.022 ms
I: plural string

The difference is that as a plural, these aren’t “linked” to each other as items. We can’t directly look for the first, second, or third of a plural because technically they are an un-ordered list. Also all elements of a Plural result have to be of the same type - we cannot combine a string and an integer, like we could with a Tuple.

We can combine tuples with plurals to get a cross-product. This matches up “each item 0” to “each item 1”:

q: ((1;2), ("three";"four"))
A: 1, three
A: 1, four
A: 2, three
A: 2, four
T: 0.068 ms
I: plural ( integer, string )

q: items 0 of ((1;2), ("three";"four"))
A: 1
A: 1
A: 2
A: 2
T: 0.041 ms
I: plural integer

q: (item 0 of it, item 1 of it) of ((1;2), ("three";"four"))
A: 1, three
A: 1, four
A: 2, three
A: 2, four
T: 0.078 ms
I: plural ( integer, string )

This process of grouping together with (item 0 of it, item 1 of it) of … is a loop (what I called an “iterator”). It takes each element of the plural result and deals with it on an individual basis. This is how we can handle concatenating plurals. We cannot concatenate a string to a plural result, but we can iterate each result and concatenate to it individually:

q: "test prefix " & ("one"; "two")
E: A singular expression is required.

q: ("test prefix " & it) of ("one"; "two")
A: test prefix one
A: test prefix two
T: 0.034 ms
I: plural string

We can also filter the results, based on any or all of the tuple items. Going back to the earlier example, we can select just the elements where the first item is a 2:

q: (item 0 of it, item 1 of it) of ((1;2), ("three";"four"))
A: 1, three
A: 1, four
A: 2, three
A: 2, four
T: 0.076 ms
I: plural ( integer, string )


q: (item 0 of it, item 1 of it) whose (item 0 of it = 2) of ((1;2), ("three";"four"))
A: 2, three
A: 2, four
T: 0.078 ms
I: plural ( integer, string )

…and then we can choose to keep only the first or second items from the tuple:

q: items 1 of (item 0 of it, item 1 of it) whose (item 0 of it = 2) of ((1;2), ("three";"four"))
A: three
A: four
T: 0.041 ms
I: plural string

So going back to the original problem, what I’m doing is creating a “plural of tuples”. I’ll add a third option here so my own laptop matches one of the conditions:

q: computer name as lowercase
A: jason-laptop
T: 0.119 ms
I: singular string

q: (((("abc";"def"), "first thing");("ghi";"jkl"), "second"); ("jason"; "bob"), "third") 
A: abc, first thing
A: def, first thing
A: ghi, second
A: jkl, second
A: jason, third
A: bob, third
T: 0.099 ms
I: plural ( string, string )

Adding a filter to just get the elements where my laptop name contains one of the strings from item 0:

q: (((("abc";"def"), "first thing");("ghi";"jkl"), "second"); ("jason"; "bob"), "third") whose (computer name as lowercase contains item 0 of it)
A: jason, third
T: 0.280 ms
I: plural ( string, string )

And in the end, I only want the second element of this tuple; we don’t need the “abc” or “def” or “jason” read back to us -

q: items 1 of (((("abc";"def"), "first thing");("ghi";"jkl"), "second"); ("jason"; "bob"), "third") whose (computer name as lowercase contains item 0 of it)
A: third
T: 0.271 ms
I: plural string

I hope this is helpful, and again the Tutorials are a great resource with real-world examples that are worth stepping through.

5 Likes

It certainly is helpful - Thanks! (and the tutorials are worth the time for sure.)