Re : Looping Through Two Lists

Re : Looping Through Two Lists

  
Hi all,

I have two advanced queries that return lists with different recordsets (List1 and List2). I can use a ForEach statement to loop through List1. However, for each 'item' in List1, I want to find a matching 'item' in List2 and process the data. Using a 3GL logic, such as C++ I could write it like this:

int loop2Start = 0;

for (int i = 0; i < List1.Length(); i++)
{
  for (int y = loop2Start ; y <= List2.Length(); i++)
  {
     if (List1[i] == List2[y])
      {
          loop2Start = y++; //this marks where to start from on next iteration;
          doSomeOtherAction(List1[i], List2[y]); //call a function to process data
          break;
      }
   }
}

How can I do the same in ServiceStudio 5.1.1.28.

Regards,

Shupi.
Hi Shupi,

You do it like this:

Here I'm not breaking out of the loop once I find a match, but if thats what you want then just put the arrow from the "DoStuff" going to the other "For Each" action.

But I strongly recommend that, if possible, you do this comparison directly in SQL through some join action.

Cheers,
Hermínio Mira
Hi Shupi,

Herminio gave to you a simple implementation of what you have but it isn't exactly what you have in your code.
To do that you need to implement all the 3 parts of the for, the initialization, the condition and the increment.

Take a look in my example:


I used a for each to iterate the first List, it's what you did in your code and then I use two assigns and an if to build the for.

We used queries to show how to do implement however I believe that you don't want to use queries, you only need to take the queries of and use the lists you want

Regards,
Jose
Hello Hermínio and Jose,

Thank you so much for your help. I have seen the light on how the implementation should be done. However, I still get an error "
Invalid Action Flow
Ambiguous paths to For Each in Action_ChangeOrder_ProductUpgrade_Create." on the first ForEach. Take a look at the following diagram:

Yes, that's right.

You cannot have a path that starts in a cycle of a for each and end after the for each. You need to return always for the foreach...

For instance, in your example, you need to link the Action_Service_CopyValuesToAnother to the second For Each.

That's the reason why I implemented the second for as I did.
Than you Jose.

Once I did that it stopped complaining.

Regards.
Shupi,

José is absolutely right about the ambiguous paths, I forgot about it in my example. But again your best bet is to put your condition inside the advanced query and then only iterate over the ones you know you need to.

Doing these types of "nested loops" will really have a big impact on performance if your lists are big, or even medium in size.

Cheers,
Hermínio Mira