288 posts
Posted 14 April 2015 - 03:45 PM
My new project is related to tables. I have a setup like this:
{
Title = {
...
}
}
I want to add a new key to it, like this:
{
Title = {
...
},
NewKey = {
...
}
}
How would I achieve this?
3057 posts
Location
United States of America
Posted 14 April 2015 - 03:46 PM
1. Open file
2. Read file
3. Unserialize to table
4. Add key
5. Serialize new table
6. Write serialized table to file
1080 posts
Location
In the Matrix
Posted 14 April 2015 - 03:47 PM
Lets say your table is named tbl, and inside of it is contained the table named Title, then to create the table named NewKey you would only have to do this:
local tbl = {Title = {} }
tbl["NewKey"] = {}
--#Or
tbl.NewKey = {} --#Can only do this for constant names.
Edit: I'm not certain if this is considered being ninja'd, since we're not entirely talking about the same thing. Yami is on the bigger picture if you're talking about tables in a file, but this is how to do it with a normal table. Although the title of the thread IS "writing key to a file" Yami is correct on the way to go, you must read, edit, then replace the file.
Edited on 14 April 2015 - 01:49 PM
288 posts
Posted 14 April 2015 - 03:53 PM
Lets say your table is named tbl, and inside of it is contained the table named Title, then to create the table named NewKey you would only have to do this:
local tbl = {Title = {} }
tbl["NewKey"] = {}
--#Or
tbl.NewKey = {} --#Can only do this for constant names.
Edit: I'm not certain if this is considered being ninja'd, since we're not entirely talking about the same thing. Yami is on the bigger picture if you're talking about tables in a file, but this is how to do it with a normal table. Although the title of the thread IS "writing key to a file" Yami is correct on the way to go, you must read, edit, then replace the file.
Yeah, It was to do with a file.