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)