Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
fishing_weights
Contributor III
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? 

thanks in advance

Labels (3)
1 Solution

Accepted Solutions
marcus_sommer

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

View solution in original post

3 Replies
marcus_sommer

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

fishing_weights
Contributor III
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)');

marcus_sommer

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