How to use a for loop to judge whether the string contains any word in the list?

57 Views Asked by At

I'd like to know whether a string contains the word from a list. For example, I have 3 strings, 120kV_xxxx, 200kV_abc, abcdefg. I'd like to choose the string started with voltage, and exclude the last one. Now my code is as below. But in python, I can give a list like ["80kV", "120kV", "200kV"], and then use a for loop to judge whether the string contains any word in the list. Is there any similar method in dm-script?

Number find_voltage_prefix(String input)
{
Number sum = -1
sum *= input.find("80kV")
sum *= input.find("120kV")
sum *= input.find("200kV")

if (sum==0) return 1
else return 0
}

Result(find_voltage_prefix("120kV_xxxx"))
1

There are 1 best solutions below

0
On BEST ANSWER

Your solution seems fine to me and there is - to my knowledge at least - no internal command that would do the same. You might want to make your routine a bit more flexible by placing the "search for" strings in a TagGroup (TagList) and iterate over it in your search (breaking on first match).

f.e.

number StringContains( string input, tagGroup searchList ){
    for (number i=0;i<searchlist.TagGroupCountTags();i++){
        string search
        if ( !searchList.TagGroupGetIndexedTagAsString(i,search) ) continue
        if  (-1 != input.find(search)) return i
    }
    return -1
}


TagGroup searchTerms = NewTagList()
searchTerms.TagGroupInsertTagAsString(Infinity(),"80kV")
searchTerms.TagGroupInsertTagAsString(Infinity(),"100kV")
searchTerms.TagGroupInsertTagAsString(Infinity(),"120kV")
string test = "I'm at 80kV"


Result("\n The string '"+test+"'...")
number foundTermIndex = test.StringContains(searchTerms) 
if ( -1 < foundTermIndex ){
    string foundTerm
    searchTerms.TagGroupGetIndexedTagAsString(foundTermIndex,foundTerm)
    Result("... does not contain the search terms '" + foundTerm + "'.")
}
else
    Result("... does not contain any of the search terms.")