Creating and Accessing a WebCore String List (Text)

string

#1

1) Give a description of the problem
Need an example of how to use the advanced variable String List (text) in the example piston below contains() function

2) What is the expected behavior?
Obtain a True/False bool value when using the function contains(string, string list) with a string list

3) What is happening/not happening?
Have tried to define, access and clear the string values of a string list without success

4) Post a Green Snapshot of the pistonimage


5) Attach any logs (From ST IDE and by turning logging level to Full)
(PASTE YOUR LOGS HERE BETWEEN THE MARKS)

REMOVE BELOW AFTER READING
If a solution is found for your question then please mark the post as the solution.


#2

You need to define which part of the list you want to check. So if rain is the 3 rd item in list it would look something like this
contains(string, string list[2])


#3

Thanks @Gopack2

I was hoping to create and use a string text list of keywords (l) to use to search a long string (k) for any of those keywords and assign True/False to a boolean variable when any of the keywords are found/or Any of the keywords Not Found. As in this example:

l = ['one', 'two', 'three']
k = 'rain in Spain falls mainly on plain'
if any(l in k): 
   rainBool = True
else: 
   rainBool = False

#4

The Contains function should satisfy why you’re trying to do:

https://wiki.webcore.co/Functions


#5

@michicago Thanks but I tried that with a single string keyword and it worked as one would expect. When the string variable or constant has ONLY ONE value and was NOT A “String List”. As far as I can tell, the contains function does not work as expected with multiple keywords in a String List (text), as in the example below (Unless I am defining the String List (text) incorrectly using *ALL and the square brackets, but the WebCore documentation on defining and using a String List is non existent in the WebCore Wiki).


#6

Ah, you’re right. I think you might need to use indexOf for that one. It’ll return the first position in the list where the text was found. If you need to see beyond that, it can be done just requires more work (and I’d need to throw together a piston to test it… won’t be able to do that for a while).

Try it without the *all and just use the variable name - it should work. If not, we’ll figure out why.


#7

Thanks @michicago.

I’m fairly confident that the indexof function will also fail using a WebCore String List (Text) as a multi-word array index to search a text string.

I did create a “Test Piston” that had a For Loop and used $index to test for the existence of each keyword in the String List[$index] and if it was found then set a boolean variable that was defined as False to True and break out of the For loop.


This For Loop, of course, worked as expected with individual elements of the array, but it painful to have to do this for pistons when I was just hoping that I could just define a multi-word keyword String List array and throw it at the WebCore contains function and have it return True if ANY of the string list array keywords are found in the Search String matched, or False if NONE.

I am also not sure about *ALL and *CLEAR as string list qualifiers as I saw in an other example. I thought that they were reserved words that accessed all the elements of a String List.


#8

Could you just use a string (not list) and then use the contains function? Seems that may be your path of least resistance.

If you need to use a list, I’ll need to get on a computer later today to sort it out.


#9

Here’s a short example - I had to change the search from ‘rain’ to ‘clear’ to get a True result though since there’s no rain in the forecast. Rain did return False as you’d expect, though:

Sample with the variables populated after execution:


#10

These are excellent examples of using the contains function with a “single” search term, like “rain” or “clear”. I can easily do this with each keyword, but when I have multiple search terms, like “rain”, “thunder”, “snow”, “hail”, “shower”, “lightning” and multiple searchStrings… the number of times I need to construct a single statement and handle the problem of setting the boolean variable to True but then not back to False on the next statement is challenging. I can also chain separate @if statements within an expression block of ‘|’ so obtain a True or False (See below Example 2), but this is messy at best in the limited editing area of the expressions GUI editor.

@michicago, I’m not saying that I cannot do this the hard way, but the original question was why can’t the contains function allow using a text string list array of search terms separated by comma’s and brackets to be used as an argument to searching the search string (Example 1)?

Example 1

arrayList[] = ["one","two",three","four","five"]
string searchString = "The Rain in Spain Falls Mainly on the plain at three o'clock"
contains(searchString, arrayList)

rather than:
Example 2

string searchString = "The Rain in Spain Falls Mainly on the plain at three o'clock"
contains(searchString, "one") | contains(searchString, "two") | contains(searchString, "three") | contains(searchString, "four") | contains(searchString, "five")

#11

Ok, now I got it. I was missing the fact you were searching for multiple terms… I blame multitasking!

I think a loop (for $index 0 to count(searchTerms)-1) is probably the most elegant solution available using the available functions. I’ll try to think of another way but don’t know of one off the top of my head.


#12

No problem, had that looping solution above (See below). Having multiple search terms and multiple searchStrings escalates things exponentially, hence the need for groovy search functions like .findall and others. Well someday…


#13

I’m trying to do what you guys are doing.
IF “weather conditions” contains either “rain”, “showers” or “thunderstorms”, then set set variable “currently raining” to true, that then sets global variable @raining to true.
So my other irrigation pistons can run off IF @raining was not true in the last X hours, then run.
I tried to use one “contains” of “rain” to set a boolean but I’m failing miserably…
@WCmore


#14

I saw this quote from @allrak in another post:


#15

Not even sure I’m needing a string list or a dynamic list but how do I set the variable? I’d like to make a list of email addresses. Do I specify them all out here and use a different index number for each one?


#16

Well that didn’t work because now it’s asking me to specify the index numbers when sending email. Basically want to create variable list of email addresses so I don’t need to keep writing them out each time.


#17

If you use a list (array) you will see brackets at the end of the variable. This means it is required to choose a number each time you refer to the variable.

Here is a basic example at work:

temp

Lines 16-18 only need to run once to populate the array.
Notice line 19 sends the email to emails[0] which is [email protected]

As far as I know, arrays are only possible when stored locally.
This means that only this piston can refer to those emails…


May I suggest scrapping arrays for emails, and simply using global variables?

temp

This lets you easily use that global variable in any piston.