lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 17 January 2012 15:29, Sandeep Ghai <sandeep.ghai92@gmail.com> wrote:
> I have read upto the tables  having two
> columns. Can anybody help me in making table that has more than two coulmns?
> like:
>
> Sr.No     x        y       Constraint     force
> 1            0        0         fixed
> -
> 2            0        120      free           sideway
> 3           120     120       free           twist
> 4           120      0        fixed             -
>
> I tried with Nested tables,but get nothing. Is nested table a good option?

It depends on how you want to use the data. If you *need* to have the
data in a matrix form (for example to do calculations on it later),
you can store the data as follows:

data = {
  {1, 0, 0, 'fixed', '-'},
  {2, 0, 120, 'free', 'sideway'},
  {3, 120, 120, 'free', 'twist'},
...
}

However, a more readable way would be to store each row as a table
with string keys, to know exactly what does each value mean, i.e.:

data = {
  {id=1, x=0, y=0, constraint='fixed', force='-'},  -- or maybe do not
include force at all
  {id=2, x=0, y=120, constraint='free', force='sideway'},
...
}

To get for example second item, you would do:

item = data[2]
print(item.x, item.y, item.constraint, item.force)