Maybe you could do something like:
X = 3; Y = 5; Table[l[i] = Indexed[g, i], {i, X}]; Table[{l[1], l[3]}, {g, Subsets[Range[Y],{X}]}] {{1, 3}, {1, 4}, {1, 5}, {1, 4}, {1, 5}, {1, 5}, {2, 4}, {2, 5}, {2, 5}, {3, 5}}
You could create a function to do this:
SetAttributes[iteratedTable,HoldAll] iteratedTable[e_, g_Symbol, iterations_, max_] := Block[{g = Indexed[\[FormalL], #]&}, Table[e, {\[FormalL], Subsets[Range@max, {iterations}]}] ] Simple example:
iteratedTable[h[l[1], l[3], l[2]], l, 3, 5] {h[1, 3, 2], h[1, 4, 2], h[1, 5, 2], h[1, 4, 3], h[1, 5, 3], h[1, 5, 4], h[2, 4, 3], h[2, 5, 3], h[2, 5, 4], h[3, 5, 4]}
Your examples:
progress = 0; iteratedTable[progress++, l, 2, 40]; progress 780
and:
progress = 0; iteratedTable[progress++, l, 3, 40]; progress 9880
An alternate approach that is similar to yours is:
SetAttributes[iteratedTable, HoldAll] iteratedTable[e_, g_, iter_, max_] := ReleaseHold @ Hold[Table][ Hold[e], Sequence @@ Table[{g[i], If[i==1, 1, g[i-1]+1], max}, {i, iter}] ]