Set of Array

Hi ,

I am new in lua. i need to set the array in table object.

eg:- [
    {
        “Name”: “John”,
        “Age”: 21
    },
    {
        “Name”: “Maclean”,
        “Age”: 26
    },
    {
        “Name”: “Letty”,
        “Age”: 28
    },
    {
        “Name”: “Nicole”,
        “Age”: 20
    }
]

how can i set this array into a table object. Any one can help me out.

I would just go with

local myTable = { {“John”, 21}, {“Maclean”, 26}, {“Letty”, 28}, {“Nicole”, 20} }

myTable[2][2] – outputs 26

RJ5’s solution work but here is another one, depending on what you want to do:

local t = {} t[1] = {} t[1].name = "John" t[1].age = 23 t[2] = {} t[2].name = "Doe" t[2].age = 0 print(t[1].name) print(t[1].age) print(t[2].name) print(t[2].age)

Best regards,

Tomas

Have a look at this page:

http://lua-users.org/wiki/TablesTutorial

I have a background in C# (which also uses arrays) so switching over to tables was a little bit different but it has some nice stuff.

Best regards,

Tomas

It might be better to do:

local myTable = { {name=“John”, Age=21}, {name=“Maclean”, Age=26}, {name=“Letty”,Age= 28}, {name=“Nicole”, Age=20} }

Then you have:  print(myTable[2].Name) would print Maclean.

Your data appears to be in JSON format so you could use the JSON library to convert it for you:

local json=require(“json”)

local myTable = json.decode(the string that holds your  jsondata)

I would just go with

local myTable = { {“John”, 21}, {“Maclean”, 26}, {“Letty”, 28}, {“Nicole”, 20} }

myTable[2][2] – outputs 26

RJ5’s solution work but here is another one, depending on what you want to do:

local t = {} t[1] = {} t[1].name = "John" t[1].age = 23 t[2] = {} t[2].name = "Doe" t[2].age = 0 print(t[1].name) print(t[1].age) print(t[2].name) print(t[2].age)

Best regards,

Tomas

Have a look at this page:

http://lua-users.org/wiki/TablesTutorial

I have a background in C# (which also uses arrays) so switching over to tables was a little bit different but it has some nice stuff.

Best regards,

Tomas

It might be better to do:

local myTable = { {name=“John”, Age=21}, {name=“Maclean”, Age=26}, {name=“Letty”,Age= 28}, {name=“Nicole”, Age=20} }

Then you have:  print(myTable[2].Name) would print Maclean.

Your data appears to be in JSON format so you could use the JSON library to convert it for you:

local json=require(“json”)

local myTable = json.decode(the string that holds your  jsondata)