Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
jwaligora
Creator II
Creator II

Table Numbering Changes During Loop Execution

Hi,

In the script, I'm looping though all the tables using an integer as the loop counter.

vTabNumber = 0 to NoOfTables()-1 

I also have an inner loop that checks if a specific field name is present. If the field is found, I do some stuff to the table (a row is added, and a join is made). 

My current problem appears to be that the table numbering changes during loop execution. This means that the outer loop on its 4th iteration seems to think that TableName(3) = MyBlueTable and again on the 6th iteration it determines that TableName(5) = MyBlueTable. Obviously this leads to chaos. 

I'm tracing my iterator variable, so I know it's incrementing correctly. I just want to understand if this is expected behaviour and if so, what types of script actions will influence the Table Name to Table Number map.

Thanks,

J.

2 Replies
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

I have run across this issue when Dropping a table in the loop. That causes the table numbers to shift down by one where the table was dropped. The fix is to walk the table list from high to low:

vTabNumber =NoOfTables()-1  to 0 Step -1;

I'm not sure if this will fix your issue when using join. I suppose it depends on where the joined-to table is in the list. 

-Rob

jwaligora
Creator II
Creator II
Author

Thanks Rob. It's good to know I'm not crazy i.e. the issue is real and not a side effect of something else I'm doing wrong. Since I wasn't adding or removing tables, just augmenting select ones, it all seemed a bit odd.

I ended up using a separate loop to deposit the table names into a list, and then running my primary loop off the table names (which are not being altered in my code), rather than the table numbers. This appears to be working.