Announcements
cancel
Showing results for
Did you mean:
Contributor III

## For loop to iterate two fields at the same time

I've a inline table

 F1 F2 1 A 2 B 3 C

I would like to loop both columns and assign a variable to them accordingly to be used. example:

For x,y in FieldValueList(F1,F2)

set vX ="x";

set vY ="y";

Trace \$(vX) and \$(vY) ;

next

The result i get should be:

1 and A

2 and B

3 and C

but the above for loop syntax is wrong. what is the right syntax?

Labels (3)

• ### Script

1 Solution

Accepted Solutions

Yes, the syntax is wrong and it wouldn't work with multiple fields at the same time - else only for a single field and then the next one and/or nesting multiple loops.

Beside this the above won't work within many scenarios because the field-values are on the field-level independent to each other. To get multiple fields synchronized you need to loop through a table, maybe like this:

for i = 0 to noofrows('MyTable') - 1
let v1 = peek('Field1, \$(i), 'MyTable');
let v2 = peek('Field2, \$(i), 'MyTable');
trace \$(v1) + \$(v2);
next

3 Replies

Yes, the syntax is wrong and it wouldn't work with multiple fields at the same time - else only for a single field and then the next one and/or nesting multiple loops.

Beside this the above won't work within many scenarios because the field-values are on the field-level independent to each other. To get multiple fields synchronized you need to loop through a table, maybe like this:

for i = 0 to noofrows('MyTable') - 1
let v1 = peek('Field1, \$(i), 'MyTable');
let v2 = peek('Field2, \$(i), 'MyTable');
trace \$(v1) + \$(v2);
next

Contributor III
Author

Thank you!  @marcus_sommer it is exactly what I needed.

interesting that this: for i = 0 to noofrows('MyTable') - 1  indicates that the index of the table starts at 0

but if I use this instead the index starts at 1 :

For Each i in FieldValueList('F1')

LET idx_i = FieldIndex('F1','\$(i)');

The same index-range and also the same notation-requirements for table/field-names everywhere would be too easy 😉