title: For a new index from two indexes in a for loop
body: <p>I was working on lua and looped over two tables and wanted to create a new table out of it, with no nil values in it. So this is basically a cross product. E.g: </p>
  
  <pre><code> {1,2,3} x {3,4,5} -&gt; {1*3,1*4,1*5,2*3,2*4,2*5,3*3,3*4,3*5}
  </code></pre>
  
  <p>Of course this is not hard to do: </p>
  
  <pre><code>  t = {1,2,3}
    s = {3,4,5}
    xs = {} 
    q = 1 
    for i,h in ipairs(t) do 
        for j,k in ipairs(s) do 
            xs[q] = h * k 
            q = q + 1 
        end 
    end 
  </code></pre>
  
  <p>We keep a counter q and add 1 every iteration. And this works fine. However is it also possible without a counter? Can I fill up x so with just i and j such that there are no gaps in x? </p>
  
  <pre><code>  t = {1,2,3}
    s = {3,4,5}
    xs = {} 
  
    for i,h in ipairs(t) do 
        for j,k in ipairs(s) do 
            q = f(i,j) -- &lt;- I want to know if f is possible to write
            xs[q] = h * k 
        end 
    end 
  </code></pre>
  
  <p>I would say not, at least I was not able to find one myself easily.</p>
  
  <p>EDIT: It is possible though if I am allowed to use the size of s. </p>
  
  <pre><code>    s = {1,2,3}
      t = {4,5,6} 
      xs = {} 
  
      for i,h in ipairs(s) do 
        for j,k in ipairs(t) do 
           q = i + (j - 1) * #t  
           xs[q] = h * k 
        end 
      end
  </code></pre>