views:

62

answers:

1

Writing a function in Lua, which creates two tables. I want the tables to be assigned to the value name with an x added, and one with a y added. For example if name was line, it would create two tables linex and liney, but I can't figure out how to do it. The following obviously doesn't work (and is just for display purposes) but how would I go about doing this?

function makelinep(x,y,minrand,maxrand,name,length)
  name..x = {}
  name..y = {}

Later I hope to access "linex" and "liney" after values have been written.

Thank you :)

+5  A: 

If you want these in the global name space you would use

_G[name..'x']={}
_G[name..'y']={}

For a module you'd use _M in place of _G.

Joel
That works perfectly, I seem to have trouble inserting values into the table though, how would go about doing this? (In the same function) table.insert(name.."x",count,x)Doesn't work.Thank you :D
Seb Horsewell
name.."x" is not a table but just a string with a name of your table. As in above example _G[name..'x'] is the table so the right syntax is table.insert(_G[name..'x'],count,x)
Ilya Martynov
Ah, makes sense! Thanks alot :D
Seb Horsewell