Need help with table

Hello i troubled with table

Here my table create
local _table = {
test1 = 0,
test2 = 0

file.Write( test.txt, util.TableToJSON( _table ) )
I trying to change value
local _file = “test.txt”
local _read = util.JSONToTable( file.Read( _file, “DATA” ))


How to change value test2 to 1, table.insert or what?

_read[“test2”] = 1, if I’m understanding you correctly.

_read['test2'] = 1

And you are also missing quotation marks in the file.write, but I am guessing you wrote this purely as an example.

[lua]_read.test2 = 1[/lua]

also works.

I undestand this, how save it to file?

file.Write it again.


But it will cahnge hole file no?

I thought you wanted it to save it to file.

I want to change value in existing table

local _file = “test.txt”
local _read = util.JSONToTable( file.Read( _file, “DATA” ))


file.Write(“test.txt”, util.TableToJSON(_read))

[editline]30th June 2014[/editline]

Seems this not right way

Correct way:
local _file = “test.txt”
local _read = util.JSONToTable( file.Read( _file, “DATA” ))

_read[‘test2’] = 1

file.Write(“test.txt”, util.TableToJSON(_read))

Fantastic example. I am going to extend this example by making a few changes to help the OP better understand when and how to use certain elements to modify / access a table elements…

// File name to read / update
local _file = "test.txt";

// _data -- We read the file, but if it doesn't exist we define a default value ( || is or then the default data "{ }" which is an EMPTY table as written in JSON so we're always given a proper table )
local _data = util.JSONToTable( file.Read( _file, "DATA" ) || "{ }" );

// Assign 1 to test2 ( simple strings can be accessed without [""] )
_data.test2 = 1

// Assign 10 to index 1 ( Any index that needs to be parsed such as a variable or a number needs []s )
_data[ 1 ] = 10;

// Assign 2 to 2test ( Any non-simple string, which are strings which start with a number or special character, needs to be contained using [""] )
_data["2test"] = 2;

// Initialize / update a nested table using the above principles
_data.nested_table = { -- simple string, we can use . to access / update it
	testing 		= 1; -- simple string
	test2 		= 2; -- simple string
	[ 3 ] 			= true; -- number, it needs to be parsed as such so [ ] is needed
	["10testing"] 	= false; -- non-simple string - we need to ensure it is parsed as a string or it'll see the number then see the string and give an error

// Update the file after converting the table back into text form ( JSON )
file.Write( _file, util.TableToJSON( _data) ); -- No need to use "DATA" because we're locked into "DATA" during write; if you look at the helper function you'll see it uses "DATA" in the code

To hopefully help you along further, here is a “tutorial” I have on the subject of files / tables:

Example of loading data ( or using default ) for purpose of saving numerically incrementing files. Saves the next index to the index file ( once without numbers ) and the data to a new file.

I do have other examples, however they are large full-code examples with MANY different principles in use, if you’d like to see them feel free to ask. They use console commands, accepting arguments, saving files, JSON, modifying tables, and more for the purpose of managing entities to delete on map load / ttt round end/begin, etc. And a donator manager using same things.