Search for a string in multiple fields

1559
7
Jump to solution
10-13-2022 08:15 AM
RichardPenman1
Occasional Contributor

I am attempting a field calculation that searches multiple fields for a specific string, in this case: apple. The string could be in any of the fields (Spring, Summer, Fall, Winter). I  want to return just one instance of the string in a new field.   

The problem I am having is that the Code Block does not like my syntax for a range of fields. See below for what is not working:

Expression

where(!Spring!)(!Summer!)(!Fall!)(!Winter!)

Code Block

def where(Spring)(Summer)(Fall)(Winter):

     if ('apple' in (Spring)(Summer)(Fall)(Winter)):

         return (Spring)(Summer)(Fall)(Winter)

     else:

        return None

 

0 Kudos
2 Solutions

Accepted Solutions
RhettZufelt
MVP Notable Contributor

This should work:

Expression
whr(!Spring!, !Summer!, !Fall!, !Winter!)

Code Block

def whr(Spring, Summer, Fall, Winter):
     if ('apple' in (Spring, Summer, Fall, Winter)):
         return "apple"
     else:
        return None

 

If apple is found in any of the fields, it will return text string "apple".

R_

View solution in original post

DavidSolari
MVP Regular Contributor

A few people have posted suggestions, here's one that works with any number of fields and any value:

def setIfFound(value, *checks):
  return value if value in checks else None

View solution in original post

7 Replies
DanPatterson
MVP Esteemed Contributor

perhaps.... and don't is key words like "where" for function names

Expression
whr(!Spring!, !Summer!, !Fall!, !Winter!)

Code Block

def whr(Spring, Summer, Fall, Winter):
     if ('apple' in (Spring, Summer, Fall, Winter):
         return (Spring, Summer, Fall,Winter)
     else:
        return None

... sort of retired...
0 Kudos
RichardPenman1
Occasional Contributor

Thank you Dan.  I added one more parenthesis on line 7 after Winter to complete that line. 

Your suggested script is an improvement because the syntax gets validated, runs, and receives no errors. Unfortunately in spite of the appearance of working, it returns NULLs and is not calculating into the new field.  If I use this same Code Block on just one field at a time it works perfectly. But adding more than one field to the search range produced NULLs. 

Also, when I attempt this with multiple fields it runs very fast. When I run it to just search one field at a time, it produces the expected output, but the process takes much longer. I assume because its actually searching.

0 Kudos
RhettZufelt
MVP Notable Contributor

What is it you want to return if "apple"is found in any of the fields?  A specific value?  Concated values from all fields?

R_

0 Kudos
RichardPenman1
Occasional Contributor

Just the name of the fruit; in this case it would be Apple. No concatenation required.  The issue I am trying to solve is that the string apple could be in any of the four fields in the range (spring, summer, etc). I want to search all four fields in one process, and have the fruit name return to a separate field

0 Kudos
RhettZufelt
MVP Notable Contributor

This should work:

Expression
whr(!Spring!, !Summer!, !Fall!, !Winter!)

Code Block

def whr(Spring, Summer, Fall, Winter):
     if ('apple' in (Spring, Summer, Fall, Winter)):
         return "apple"
     else:
        return None

 

If apple is found in any of the fields, it will return text string "apple".

R_

DavidSolari
MVP Regular Contributor

A few people have posted suggestions, here's one that works with any number of fields and any value:

def setIfFound(value, *checks):
  return value if value in checks else None
RhettZufelt
MVP Notable Contributor

I like it.  I had forgotten about being able to load multiple variables in there with the asterisk......

R_

0 Kudos