Can I use a For Each loop to compare 2 values within the collection?

Hi,

Is it possible to compare an item in a collection to the very next item using a FOR EACH loop?

I’d like to do something like this but I’m not sure what the syntax would be:

For Each Item in Collection

If item = item+1 Then…

Here, “item” = the initial item and “Item+1” is the value of the item next iteration of the loop.

Thanks!

Hi @SentientPizza

For that you have to take some temp variable to store the previous value and then use the comparision logic

Happy Automation :raised_hands:

Best Regards
Er Pratik Wavhal :robot::man_technologist:t4: :computer:

@SentientPizza
as it is in a collection you can organize the loop over the indexes.

Lets assume you have a list with 10 elements

use a for each
type argument is Int32
for the iteration pass Enumerable.Range(1, (YourList.Count - 1)).toList - it is creating a sequence starting by 1,2,3,4…
we keep the name of loop var - item

in the loop block the compare can be done with
if: YourList.ElementAt(item-1) = YourList.ElementAt(item)

Great, thanks for the help both of you; I was able to get my loop up/running with this!