title: Can I use a value stored in a table as a key in another table?
body: <p>I'm new to LUA and I still haven't gotten the hang of how classes work in LUA, so my question
  probably has a very simple answer. I'm trying to make a function that takes a CSV file and turns it into a lua table.</p>
  <p>The input file would be something like this</p>
  <pre><code>PropertyKey1,Propertykey2,Propertykey3
  object1property1,object1property2,object1property3
  object2property1,object2property2,object2property3
  object3property1,object3property2,object3property3
  </code></pre>
  <p>and I want the resulting lua table to look something like this</p>
  <pre><code>objects = {
      {
        PropertyKey1 = object1property1
        PropertyKey2 = object1property2
        PropertyKey3 = object1property3
      }
      {
        PropertyKey1 = object2property1
        PropertyKey2 = object2property2
        PropertyKey3 = object2property3
      }
      {
        PropertyKey1 = object3property1
        PropertyKey2 = object3property2
        PropertyKey3 = object3property3
      }
  }
  </code></pre>
  <p>this is what I have thus far</p>
  <pre><code>function loadcsv(path)
    local OutTable = {}
    local file = io.open(path, &quot;r&quot;)
    local linecount = 0
  
    for line in file:lines() do
      local data = {}
      local headers = {}
      local headerkey = 1
  
      if linecount &lt; 1 then
        for val in line:gmatch(&quot;([^,]+),?&quot;) do
          table.insert(headers, val)
        end
      else
        for word in line:gmatch(&quot;([^,]+),?&quot;) do
          key = headers[headerkey]
          data[headerkey] = word
          headerkey = headerkey + 1
          table.insert(OutTable, data)
        end
      end
  
      linecount = linecount + 1
    end
  
    file:close()
    return OutTable
  end
  </code></pre>
  <p>The above code does not run. When I try to print any of the values, they come as nil.
  The problem is this bit</p>
  <pre><code>        key = headers[headerkey]
          data[headerkey] = word
  </code></pre>
  <p>I wanted to use the values I stored in one table as keys on the second table, but it looks like since LUA only passes the references, that doesn't work.</p>
  <p>I did a quick experiment to confirm it. I first set up 2 tables.</p>
  <pre><code>  test = {}
    test2 = {}
    test[1]={&quot;index&quot;}
    key = test[1]
    key2 = &quot;index&quot;
  
  </code></pre>
  <p>First I tried assigning the value directly form the table</p>
  <pre><code>  test2[test[1]] = &quot;text&quot;
    print(test2.index)        --This did not work
  </code></pre>
  <p>then I tried going trough another variable</p>
  <pre><code>  test2[key] = &quot;texto&quot;
    print(test2.index)        --This did not work
  </code></pre>
  <p>I even tried using tostring()</p>
  <pre><code>  key = tostring(test[1])
    test2[key] = &quot;texto&quot;
    print(test2.index)        --This did not work
  </code></pre>
  <p>I wrote the string directly in the variable &quot;key2&quot; to confirm that I was using the right notation.</p>
  <pre><code>  test2[key2] = &quot;text&quot;
    print(test2.index)        --This one worked
  </code></pre>
  <p>I read a bit on metatables, but I'm not fully clear on those. Would that be the simplest way to do what I'm trying to do, or is my approach flawed in some other way?</p>