How can I use iterator in For loop?

I tried using this but it failed.

a = {1,2,3,4,5,6} local function testFunc() for a[1] = 1,6 do --Do something end end

It keeps showing the message ‘=’ or ‘in’ expected near ‘[’ and when I tried using while loop instead, it told me that I compared the number and nil value !? Could anyone help me figure this out or suggest the alternative ways to do things like this?

[lua]

for i = 1, #a, 1 do

 print ("Entry “…i…” in table is "…a[i])

end

[/lua]

This will loop through the 6 entries in your ‘a’ table.

Hi Nick, Thank for your reply but I want to use a[1] because I want something like 

for i=1,10 do for j=1,10 do for k=1,10 do . . . for p=1,10 do -- Do something end end end end

So, I want to store the iterator in table form because I don’t want to hard code like that. And with that from, it’s easier to read :slight_smile:

This will iterate through the loops depending on the values set in table ‘a’:

[lua]

for i = 1 , a[1], 1 do

      for j = 1 , a[2], 1 do

           

            for k = 1 , a[3], 1 do

                  – do something

                 

            end

      end

end

[/lua]

If the table values must be the iterator, you could try:

[lua]

repeat

    

      a[1] = a[1] + 1

      repeat

            a[2] = a[2] + 1

            – do something

      until a[2] = 10

until a[1] = 6

[/lua]

I haven’t used nested repeat loops before so not sure they behave the same as nested for loops.

How about this …

num\_loops = 3 loop\_vals = {0,0,0} loop\_maxs = {3,3,4} function next\_iter() outp = {} for i = 1,num\_loops do outp[i] = loop\_vals[i] end -- look for sentinel value in last position if( loop\_vals[num\_loops] == (loop\_maxs[num\_loops]+1) ) then return nil end for i = 1,num\_loops do loop\_vals[i] = loop\_vals[i] + 1 if( loop\_vals[i] == loop\_maxs[i] ) then loop\_vals[i] = 0 if( i == num\_loops ) then -- put the sentinel in place for next iter loop\_vals[i] = loop\_maxs[i] + 1 end else -- we're done break end end return outp end while( 1 ) do vals = next\_iter() if( vals == nil ) then break end txt = '' for i=1,num\_loops do txt = txt .. vals[i] .. ',' end print( txt ) end

The output is …

0,0,0, 1,0,0, 2,0,0, 0,1,0, 1,1,0, 2,1,0, 0,2,0, 1,2,0, 2,2,0, 0,0,1, 1,0,1, 2,0,1, 0,1,1, 1,1,1, 2,1,1, 0,2,1, 1,2,1, 2,2,1, 0,0,2, 1,0,2, 2,0,2, 0,1,2, 1,1,2, 2,1,2, 0,2,2, 1,2,2, 2,2,2, 0,0,3, 1,0,3, 2,0,3, 0,1,3, 1,1,3, 2,1,3, 0,2,3, 1,2,3, 2,2,3,

(it starts counting with 0 up to n-1, you might need to adjust the sentinel value if you want 1 to n instead)

Thank you very much Nick and jbp1, I’ve successfully done this task finally. :smiley:

[lua]

for i = 1, #a, 1 do

 print ("Entry “…i…” in table is "…a[i])

end

[/lua]

This will loop through the 6 entries in your ‘a’ table.

Hi Nick, Thank for your reply but I want to use a[1] because I want something like 

for i=1,10 do for j=1,10 do for k=1,10 do . . . for p=1,10 do -- Do something end end end end

So, I want to store the iterator in table form because I don’t want to hard code like that. And with that from, it’s easier to read :slight_smile:

This will iterate through the loops depending on the values set in table ‘a’:

[lua]

for i = 1 , a[1], 1 do

      for j = 1 , a[2], 1 do

           

            for k = 1 , a[3], 1 do

                  – do something

                 

            end

      end

end

[/lua]

If the table values must be the iterator, you could try:

[lua]

repeat

    

      a[1] = a[1] + 1

      repeat

            a[2] = a[2] + 1

            – do something

      until a[2] = 10

until a[1] = 6

[/lua]

I haven’t used nested repeat loops before so not sure they behave the same as nested for loops.

How about this …

num\_loops = 3 loop\_vals = {0,0,0} loop\_maxs = {3,3,4} function next\_iter() outp = {} for i = 1,num\_loops do outp[i] = loop\_vals[i] end -- look for sentinel value in last position if( loop\_vals[num\_loops] == (loop\_maxs[num\_loops]+1) ) then return nil end for i = 1,num\_loops do loop\_vals[i] = loop\_vals[i] + 1 if( loop\_vals[i] == loop\_maxs[i] ) then loop\_vals[i] = 0 if( i == num\_loops ) then -- put the sentinel in place for next iter loop\_vals[i] = loop\_maxs[i] + 1 end else -- we're done break end end return outp end while( 1 ) do vals = next\_iter() if( vals == nil ) then break end txt = '' for i=1,num\_loops do txt = txt .. vals[i] .. ',' end print( txt ) end

The output is …

0,0,0, 1,0,0, 2,0,0, 0,1,0, 1,1,0, 2,1,0, 0,2,0, 1,2,0, 2,2,0, 0,0,1, 1,0,1, 2,0,1, 0,1,1, 1,1,1, 2,1,1, 0,2,1, 1,2,1, 2,2,1, 0,0,2, 1,0,2, 2,0,2, 0,1,2, 1,1,2, 2,1,2, 0,2,2, 1,2,2, 2,2,2, 0,0,3, 1,0,3, 2,0,3, 0,1,3, 1,1,3, 2,1,3, 0,2,3, 1,2,3, 2,2,3,

(it starts counting with 0 up to n-1, you might need to adjust the sentinel value if you want 1 to n instead)

Thank you very much Nick and jbp1, I’ve successfully done this task finally. :smiley: