2

I got a table of form let's say:

house = {
             ["Street 22"] = {
                        {name = "George", age = 20},
                        {name = "Pete", age = 25}
                      },
             ["Street 30"] = {
                        {name = "John", age = 32},
                    }
           }

And I want to insert programmatically a third house, that is key "Street 35", with a person's details, Nick and 30 let's say. I am relatively new to lua and don't know how to do this, I must use table.insert but I am having troubles following the above format... Some help please?

1
  • 2
    house["Street 35"] = house["Street 35"] or {}; table.insert(house["Street 35"], {name = "Nick", age = 30}) Commented Jan 1, 2017 at 6:45

3 Answers 3

6

Do it simple so:

house["Street 52"] = {{name = "Nick", age = 30}}
Sign up to request clarification or add additional context in comments.

Comments

3

did you read this and get confused?

just try table.insert(house, {name = "Nick", age = 30}) and house[3] now contains the new element.

1 Comment

Thanks, that works, it was simple indeed. But I edited my question a bit, what if I need to add a key "Street 52" for example with name and age the ones I mention...?
3

You could also mutate the third element of the houses table like this:

house[3]={name = "Nick", age = 30}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.