Foreach loop with index

I am trying out the foreach loop on a simple table. The action flow and table are in the screenshot below:

The output I get is:

foreachindex

When the index is specified, the output comes as the value within the square brackets []. Why isn’t just the value output in this case (doesn’t the index also point to the specific element in the list and is the same as the nested foreach for each item in a row)?

Hi, run another for loop with that index and you should only see the value without the brackets

If I understand correctly you are saying there needs to be another foreach element in [object obtained by using the index], within which the element variable will have the final value.
How do I make the index as the list/table variable for the for loop?

post your script here, will add the loop and show you, if my understanding of your request is correct

enableTypeOnScreen();

openAndFocus("notepad", 5000, 300);

def i0 = data
if (data instanceof com.workfusion.studio.rpa.recorder.api.groovy.RecorderTable) {
i0 = data.getRowsAsListWrappers()
}

new com.workfusion.studio.rpa.recorder.api.groovy.FilterExpression(“2,3”).applyTo(i0).forEach( { row ->
def i1 = row
if (row instanceof com.workfusion.studio.rpa.recorder.api.groovy.RecorderTable) {
i1 = row.getRowsAsListWrappers()
}

new com.workfusion.studio.rpa.recorder.api.groovy.FilterExpression("*").applyTo(i1).forEach( { item ->

        def i2 = item;

		if (item instanceof com.workfusion.studio.rpa.recorder.api.groovy.RecorderList) {
			i2 = new com.workfusion.studio.rpa.recorder.api.groovy.FilterExpression("1").applyTo(item);

		} else if (item instanceof com.workfusion.studio.rpa.recorder.api.groovy.RecorderTable) {
			i2 = new com.workfusion.studio.rpa.recorder.api.groovy.FilterExpression("1").applyTo(item);

		} else if (item instanceof BigDecimal) {
			i2 = item.toPlainString();
		}

        sendKeys(com.workfusion.studio.rpa.recorder.api.groovy.StringTransformations.escapeAutoitText(String.valueOf(i2)));

    


    sendKeys(com.workfusion.studio.rpa.recorder.api.groovy.StringTransformations.escapeAutoitText(" "));

});

    sendKeys(com.workfusion.studio.rpa.recorder.api.groovy.StringTransformations.escapeAutoitText(" "));

    


        def i3 = row;

		if (row instanceof com.workfusion.studio.rpa.recorder.api.groovy.RecorderList) {
			i3 = new com.workfusion.studio.rpa.recorder.api.groovy.FilterExpression("2").applyTo(row);

		} else if (row instanceof com.workfusion.studio.rpa.recorder.api.groovy.RecorderTable) {
			i3 = new com.workfusion.studio.rpa.recorder.api.groovy.FilterExpression("2").applyTo(row);

		} else if (row instanceof BigDecimal) {
			i3 = row.toPlainString();
		}

        sendKeys(com.workfusion.studio.rpa.recorder.api.groovy.StringTransformations.escapeAutoitText(String.valueOf(i3)));

    


    sendKeys(com.workfusion.studio.rpa.recorder.api.groovy.StringTransformations.getKeyPressText(28, 13, 10, 0));

});

can you export the recording in a zip file and upload it here please

foreachtest.zip (1.8 KB)

please test and see if it workforeachtest.zip (2.1 KB)

image

1 Like

filter for 2 at the item1 loop, remove all other filters

1 Like

@gerhardus_meyer thanks. This is working.

My confusion was that using the Index I thought it gives the actual value but that still returns a List which needs to be looped further to get the elements.

@mprateek Yes, that’s right. For Each (row) always returns a list, even if you use index and there is only one element in the list.

Looks like a reinventing the wheel. I think that accessing a value from row by index should return the plain cell value without any square brackets.

1 Like

@mykhailo_hariachyi
Hi Mykhailo,
Yes, we’ll fix the issue with square brackets in the future releases. For now, the workaround suggested by @gerhardus_meyer is the best way to go.

Alesia

@mprateek @mykhailo_hariachyi @gerhardus_meyer
This issue has been fixed in RPA Express 1.2.0 - index inside For Each loop action returns a value without square brackets.

1 Like

Hello,

I am scripting one flow wherein I need to enter values present in the table to a web form.

I have used for each function to submit the same form multiple times.

Problem statement :-

I don’t see any index feature in the Web element action, and hence when I set the variable to the ${item} pointing to the table, the entire column data gets set to that field I am pointing to.

Any workaround here?

Thanks,
Harmeet

@harmeet_singhn You need to include another for each loop as shown in the post above to iterate through elements in the row.