Forum Discussion

RUDOLF_BOTHMA's avatar
RUDOLF_BOTHMA
Community Hero
6 years ago
Solved

Use .Find to get all elements that do NOT match the search properties

Hi all,

 

I'm trying to find a way to make my code less loopy and possibly improve the performance.  I'm trying to take a list of items, get rid of the items that I'm not interested in, and keep the rest

 

Example items*:

Red Tomato

Orange Carrot

Red Strawberry

Green Lettuce

Red Cherry

...

 

I wan't to find all the fruit/veg that aren't red.  I don't know how many fruit or vegetables there are that aren't red - this can vary - but I expect that the majority of the fruit/veg will be red.  I can't go through all the colours that aren't red and find them one by one and add them, because I don't know which colours of fruit/veg there will be.  I could find ALL the fruit/veg then loop through those objects and remove everything that's red - which is what I generally do now - but it seems a waste to return such a large amount of items if all I need to do is use a NOT filter, which will return two or three of the 50 items.  Is there a way to do that ?

 

*Surnames removed to protect identities

 

 

 

  • Hi,

     

    The below may appear to be not relevant for your real case because I don't know how color is determined, but as an idea you may consider using regular expressions for search parameters.

    Sample pseudocode:

    var objects = root.FindAllChildren(['Type', 'Color'], ['regexp: (vegetable)|(fruit)', 'regexp:^(red)'], depth);

     

  • That is SOO exactly what I was looking for, thanks ! :smileyvery-happy:

    Syntax correction:

    It's 'regexp:[^(red)]' - it was just missing the [] around the ^(red)

     

5 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    The below may appear to be not relevant for your real case because I don't know how color is determined, but as an idea you may consider using regular expressions for search parameters.

    Sample pseudocode:

    var objects = root.FindAllChildren(['Type', 'Color'], ['regexp: (vegetable)|(fruit)', 'regexp:^(red)'], depth);

     

    • RUDOLF_BOTHMA's avatar
      RUDOLF_BOTHMA
      Community Hero

      That is SOO exactly what I was looking for, thanks ! :smileyvery-happy:

      Syntax correction:

      It's 'regexp:[^(red)]' - it was just missing the [] around the ^(red)

       

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        That is SOO exactly

        Wow, great to hear! :)

         

        Syntax correction:

        May be, needs to be checked.

        To my understanding, [red] will match if the string been processed contains either 'r', or 'e', or 'd'.

        While (red) should match 'red' exactly.

        But I might be wrong and, as I said, exact syntax needs verification.