This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
_removed's profile picture

Writing a key to a file

Started by _removed, 14 April 2015 - 01:45 PM
_removed #1
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?
KingofGamesYami #2
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
Dragon53535 #3
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
_removed #4
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.