2
votes

I'm working on exporting the contents of a Lua table to a HTML File so I can display the contents in the browser. Right now I'm having problems with passing a function argument as a table key.

I have a sparse table as such:

map = {}
for x = 1, 20 do
    map[x] = {}
    for y = 1, 20 do
       map[x][y] = {}
       map[x][y].node = math.random(1,20)
       map[x][y].image = "path/to/image.png"
    end
end

I pass the table to my function as such: htmParser:_dumpSparseToHTML(map, 20, 20) where map = table I want to pass, 20,20 = width and height of the array. Somewhere in _dumpSparseToHTML I write the values of v.node and v.image to the file. How do I handle the exact same thing without knowing the name of the keys in the table? For example map could contain map[x][y].value, map[x][y].gfx, map[x][y].nodeType, and I would like to pass them as htmParser:_dumpSparseToHTML(map, 20, 20, value, gfx, nodeType, etc).

I know that Lua can handle a variable number of arguments, by defining the function as: _dumpSparseToHTML(map, 20, 20, ...). I tried to do the following:

--_table = map
for i,v in ipairs(arg) do 
     file:write("<td>".._table[x][y].v.."</td>)
end

The error I get is: "attempt to concatenate field 'v' (a nil value). So, my question is: how do I pass a variable number of arguments as table keys?

1

1 Answers

5
votes

You need to use _table[x][y][v] for that. _table[x][y].v is _table[x][y]["v"].