How to modify info within a table?

I’m trying to modify the amount of players using rawset(), table.insert(), tostring…
basically anything I think would be possibe, but my results were not like the original table that was saved with robmiracles loadsave code.

Here’s the table

[code]local player = {}

player[1]= {}
player[1].name = “John”
player[1].team = “blue”
player[1].teamMembers=15

player[2]= {}
player[2].name = “Kate”
player[2].team = “red”
player[2].teamMembers=5

player[3]= {}
player[3].name= “Steve”
player[3].team = “orange”
player[3].teamMembers=8

player[4]= {}
player[4].name = “Mike”
player[4].team = “black”
player[4].teamMembers=10
[/code]
here’s the data saved on the json file:
[{“team”:“blue”,“name”:“John”,“teamMembers”:15},{“team”:“red”,“name”:“Kate”,“teamMembers”:5},{“team”:“orange”,“name”:“Steve”,“teamMembers”:8},{“team”:“black”,“name”:“Mike”,“teamMembers”:10}]

this is the data saved after my attempt in modifying it… using
“rawset( player, 2, teamMembers, 17)”

[{“team”:“blue”,“name”:“John”,“teamMembers”:15},null,{“team”:“orange”,“name”:“Steve”,“teamMembers”:8},{“team”:“black”,“name”:“Mike”,“teamMembers”:10}]

I’m pretty new with this but learning a lot for the past few months! I’m greatful for any help I can get!
Thank You,
Tony [import]uid: 169633 topic_id: 31958 reply_id: 331958[/import]

@tonyv1124

Newb here, your mileage may vary.

I stumbled on Rob’s loadsave module a couple of weeks ago and have been using it constantly with my current project. I haven’t actually seen a post by Rob promoting it, I may have missed the post though. Thanks for the module Rob!

I’m not sure if this is the most elegant coding, but it works and will let you modify and basically do anything you’d like to do to a table.

My strategy uses a temporary table, “TempTable”, to access the saved json file and modify the table it contains. I constantly re-use the TempTable , From my experience, I’ve found there is no need to clear, delete, or nil the table once it’s been declared, just make sure to save the json file once any modifications have been made. If anyone sees an error with this statement, please feel free to correct me.

Run this in the simulator, it should work.

--modify table and load/save with Rob's loadsave module  
local json = require("json")  
local loadsave = require("loadsave")  
local TempTable ={}   
  
local player = {}  
   
player[1]= {}  
player[1].name = "John"  
player[1].team = "blue"  
player[1].teamMembers=15  
   
player[2]= {}  
player[2].name = "Kate"  
player[2].team = "red"  
player[2].teamMembers=5  
   
player[3]= {}  
player[3].name= "Steve"  
player[3].team = "orange"  
player[3].teamMembers=8  
   
player[4]= {}  
player[4].name = "Mike"  
player[4].team = "black"  
player[4].teamMembers=10  
local function printNewTableIndexValues() --test print new indexed table to see if the code below works  
 TempTable = loadsave.loadTable("playerTable.json")  
 print("TempTable[5].message == ", TempTable[5].message)  
  
 --prints new table value: TempTable[5].message ==   
 -- Thanks Rob for your loadsave Module :)   
end  
  
local function addNewPlayerTable() --adding new sequential table index to playerTable  
 TempTable[5] = {}  
 TempTable[5].name = "Rob"  
 TempTable[5].team = "green"  
 TempTable[5].teamMembers = 25  
 TempTable[5].message = "Thanks Rob for your loadsave Module :)"  
  
 loadsave.saveTable(TempTable, "playerTable.json")  
  
 printNewTableIndexValues()  
end  
  
local function printTableVariables() --load modified table and print test  
 TempTable = loadsave.loadTable("playerTable.json")  
 print("TempTable[2].teamMembers == ", TempTable[2].teamMembers)  
 --prints modified valus: TempTable[2].teamMembers == 17  
  
 addNewPlayerTable()  
end  
  
local function modifyTable() -- modify and save existing table  
 TempTable = loadsave.loadTable("playerTable.json")  
  
 TempTable[2].teamMembers = 17  
  
 loadsave.saveTable(TempTable, "playerTable.json")  
  
 printTableVariables()  
end  
  
local function saveTableToDirectory() --initially encode and load table to directory  
 loadsave.saveTable(player, "playerTable.json")  
 modifyTable()  
end  
  
saveTableToDirectory()  

I hope this helps.

Nail [import]uid: 106779 topic_id: 31958 reply_id: 127412[/import]

@tonyv1124

Newb here, your mileage may vary.

I stumbled on Rob’s loadsave module a couple of weeks ago and have been using it constantly with my current project. I haven’t actually seen a post by Rob promoting it, I may have missed the post though. Thanks for the module Rob!

I’m not sure if this is the most elegant coding, but it works and will let you modify and basically do anything you’d like to do to a table.

My strategy uses a temporary table, “TempTable”, to access the saved json file and modify the table it contains. I constantly re-use the TempTable , From my experience, I’ve found there is no need to clear, delete, or nil the table once it’s been declared, just make sure to save the json file once any modifications have been made. If anyone sees an error with this statement, please feel free to correct me.

Run this in the simulator, it should work.

--modify table and load/save with Rob's loadsave module  
local json = require("json")  
local loadsave = require("loadsave")  
local TempTable ={}   
  
local player = {}  
   
player[1]= {}  
player[1].name = "John"  
player[1].team = "blue"  
player[1].teamMembers=15  
   
player[2]= {}  
player[2].name = "Kate"  
player[2].team = "red"  
player[2].teamMembers=5  
   
player[3]= {}  
player[3].name= "Steve"  
player[3].team = "orange"  
player[3].teamMembers=8  
   
player[4]= {}  
player[4].name = "Mike"  
player[4].team = "black"  
player[4].teamMembers=10  
local function printNewTableIndexValues() --test print new indexed table to see if the code below works  
 TempTable = loadsave.loadTable("playerTable.json")  
 print("TempTable[5].message == ", TempTable[5].message)  
  
 --prints new table value: TempTable[5].message ==   
 -- Thanks Rob for your loadsave Module :)   
end  
  
local function addNewPlayerTable() --adding new sequential table index to playerTable  
 TempTable[5] = {}  
 TempTable[5].name = "Rob"  
 TempTable[5].team = "green"  
 TempTable[5].teamMembers = 25  
 TempTable[5].message = "Thanks Rob for your loadsave Module :)"  
  
 loadsave.saveTable(TempTable, "playerTable.json")  
  
 printNewTableIndexValues()  
end  
  
local function printTableVariables() --load modified table and print test  
 TempTable = loadsave.loadTable("playerTable.json")  
 print("TempTable[2].teamMembers == ", TempTable[2].teamMembers)  
 --prints modified valus: TempTable[2].teamMembers == 17  
  
 addNewPlayerTable()  
end  
  
local function modifyTable() -- modify and save existing table  
 TempTable = loadsave.loadTable("playerTable.json")  
  
 TempTable[2].teamMembers = 17  
  
 loadsave.saveTable(TempTable, "playerTable.json")  
  
 printTableVariables()  
end  
  
local function saveTableToDirectory() --initially encode and load table to directory  
 loadsave.saveTable(player, "playerTable.json")  
 modifyTable()  
end  
  
saveTableToDirectory()  

I hope this helps.

Nail [import]uid: 106779 topic_id: 31958 reply_id: 127412[/import]

xnailbender
Thanks, I’ll try it out!
update:
Works beautifully!
Thanks again! [import]uid: 169633 topic_id: 31958 reply_id: 127427[/import]

@tonyv1124 wrote:
Works beautifully!
Thanks again!

Nice! I personally struggled with this for quite awhile, I wish there was an example similar to my post when I first started. Your title is perfect and should be easily found with Google by others that start playing with Corona.

Thanks for the update, it’s always nice hear the posting effort was worthwhile.

Nail [import]uid: 106779 topic_id: 31958 reply_id: 127824[/import]

You all are quite welcome. People think they need to know JSON to use JSON and I wrote this to simply give everyone a quick way to save a table of settings, scores whatever out and read it back in without having to deal with parsing. I’m glad it’s being beneficial.

The problem I think the Original Poster is running into has to do with using rawset() (a call I’ve never used before).

It seems to want to write a single value to a table at a given index. However your table player is made up of sub-tables, not singular values.

“rawset( player, 2, teamMembers, 17)”

doesn’t do

player[2].teamMembers = 17

But instead does:

players[2] = teamMembers

and the 17 is ignored. Since teamMembers has not been defined, it’s value is nil and players[2] becomes nil, erasing the sub-table that was there. I’m not sure why you’re using rawset, when you can simply do:

players[2].teamMembers = 20

FWIW if you have a table:

t[1] = 5
t[2] = 3
t[3] = nil
t[4] = 7
t[5] = 10

Getting the length of the table using the # operator stops at the nil… thus

print(#t)

results in “2”, not “5”. The api call “table.maxn(t)” would return 5 in this case. [import]uid: 19626 topic_id: 31958 reply_id: 127830[/import]

xnailbender
Thanks, I’ll try it out!
update:
Works beautifully!
Thanks again! [import]uid: 169633 topic_id: 31958 reply_id: 127427[/import]

@tonyv1124 wrote:
Works beautifully!
Thanks again!

Nice! I personally struggled with this for quite awhile, I wish there was an example similar to my post when I first started. Your title is perfect and should be easily found with Google by others that start playing with Corona.

Thanks for the update, it’s always nice hear the posting effort was worthwhile.

Nail [import]uid: 106779 topic_id: 31958 reply_id: 127824[/import]

You all are quite welcome. People think they need to know JSON to use JSON and I wrote this to simply give everyone a quick way to save a table of settings, scores whatever out and read it back in without having to deal with parsing. I’m glad it’s being beneficial.

The problem I think the Original Poster is running into has to do with using rawset() (a call I’ve never used before).

It seems to want to write a single value to a table at a given index. However your table player is made up of sub-tables, not singular values.

“rawset( player, 2, teamMembers, 17)”

doesn’t do

player[2].teamMembers = 17

But instead does:

players[2] = teamMembers

and the 17 is ignored. Since teamMembers has not been defined, it’s value is nil and players[2] becomes nil, erasing the sub-table that was there. I’m not sure why you’re using rawset, when you can simply do:

players[2].teamMembers = 20

FWIW if you have a table:

t[1] = 5
t[2] = 3
t[3] = nil
t[4] = 7
t[5] = 10

Getting the length of the table using the # operator stops at the nil… thus

print(#t)

results in “2”, not “5”. The api call “table.maxn(t)” would return 5 in this case. [import]uid: 19626 topic_id: 31958 reply_id: 127830[/import]

Thank you so much for the info robmiracle! That helps a lot!

When it comes to deleting a player and its data, how would this be done using the TempTable method? Or is it to be done another way?

Thanks again! [import]uid: 169633 topic_id: 31958 reply_id: 128861[/import]

@tony wrote:
When it comes to deleting a player and its data, how would this be done using the TempTable method? Or is it to be done another way?

I think this really depends on your code and how you are accessing/using the tables and iterating through them.

I’m out of my league to give out instruction relating to table deletion and nilling out indexed tables and don’t want to create problems for you because of my lack of knowledge on table manipulation and how this would work with your code.

Here’s how I handle clearing data and a little insight into why.

I have player buttons 1, 2, & 3 that reference player[1].ID = 1, player[2].ID = 2, and player[3].ID = 3.

The table index matches the ID so I never nil player[2] table-- player[2] = nil – creating a hole in my table.

In order to clear a player’s data, I only reset the table to it’s original state when the app was first loaded on the device.

For the properties that are displayed in text on the screen, I sometimes use an empty string " ", other times I replace the string with “Empty” or “None” depending on what I want to see on the screen and how I want to access and utilize the property.

TempTable[2].ID = 2
TempTable[2].name = " "
TempTable[2].team = “None”
TempTable[2].teamMembers=0

When player 2 button is pressed from the menu screen my code does something like this.
snip…

local PlayerID = 0  
  
local function loadPlayerProperties()  
 if player[PlayerID].team == "None" then -- I use the PlayerID as the index number now  
 enterTeamName() --starts the process to create a new player  
 else  
 startGame()  
 end  
end  
  
  
local function player2ButtonPressed(event)  
 if event.phase == "ended" then  
 PlayerID = 2 -- I use the PlayerID as the player's table index number from here on out  
 loadPlayerProperties()  
 end  
end  

Hope this helps,

Nail

[import]uid: 106779 topic_id: 31958 reply_id: 128876[/import]

Thank you so much for the info robmiracle! That helps a lot!

When it comes to deleting a player and its data, how would this be done using the TempTable method? Or is it to be done another way?

Thanks again! [import]uid: 169633 topic_id: 31958 reply_id: 128861[/import]

@tony wrote:
When it comes to deleting a player and its data, how would this be done using the TempTable method? Or is it to be done another way?

I think this really depends on your code and how you are accessing/using the tables and iterating through them.

I’m out of my league to give out instruction relating to table deletion and nilling out indexed tables and don’t want to create problems for you because of my lack of knowledge on table manipulation and how this would work with your code.

Here’s how I handle clearing data and a little insight into why.

I have player buttons 1, 2, & 3 that reference player[1].ID = 1, player[2].ID = 2, and player[3].ID = 3.

The table index matches the ID so I never nil player[2] table-- player[2] = nil – creating a hole in my table.

In order to clear a player’s data, I only reset the table to it’s original state when the app was first loaded on the device.

For the properties that are displayed in text on the screen, I sometimes use an empty string " ", other times I replace the string with “Empty” or “None” depending on what I want to see on the screen and how I want to access and utilize the property.

TempTable[2].ID = 2
TempTable[2].name = " "
TempTable[2].team = “None”
TempTable[2].teamMembers=0

When player 2 button is pressed from the menu screen my code does something like this.
snip…

local PlayerID = 0  
  
local function loadPlayerProperties()  
 if player[PlayerID].team == "None" then -- I use the PlayerID as the index number now  
 enterTeamName() --starts the process to create a new player  
 else  
 startGame()  
 end  
end  
  
  
local function player2ButtonPressed(event)  
 if event.phase == "ended" then  
 PlayerID = 2 -- I use the PlayerID as the player's table index number from here on out  
 loadPlayerProperties()  
 end  
end  

Hope this helps,

Nail

[import]uid: 106779 topic_id: 31958 reply_id: 128876[/import]

Interesting… Thank you for the insight!
I will tinker around with it!
[import]uid: 169633 topic_id: 31958 reply_id: 129336[/import]

Interesting… Thank you for the insight!
I will tinker around with it!
[import]uid: 169633 topic_id: 31958 reply_id: 129336[/import]