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

Opening Table updating the table then saving it.

Started by SNWLeader, 24 November 2014 - 01:46 AM
SNWLeader #1
Posted 24 November 2014 - 02:46 AM
I am having a problem with making a setting update portion. I made a small program to test this before importing it.
I get it to read the file and save it, but seems to not change any of the settings.

could I get some help on that?
here is what I have


function load(dest)					 --This loads the information located at "dest".
local file = fs.open(dest,"r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end
function save(table,dest) --This saves the table to file dest.
if fs.exists(dest) == true then
fs.delete(dest)
end
local file = fs.open(dest,"w")
file.write(textutils.serialize(table))
file.close()
end
local infoTable = load("info")
term.clear()
term.setCursorPos(1,1)
newTable = {username = infoTable["username"],pwd = infoTable["pwd"],APIv = infoTable["APIv"]}
print("username:  "..newTable["username"])
print("password:  "..newTable["pwd"])
write("?: ")
input = read()
if input == "username" then
term.clear()
term.setCursorPos(1,1)
write("?:  ")
input = read()
newTable["username"] = input
elseif input == "password" then
term.clear()
term.setCursorPos(1,1)
write("?:  ")
input = read()
newTable["pwd"] = input
save(newTable,"info")
end
term.clear()
term.setCursorPos(1,1)
print("Info has been changed")
KingofGamesYami #2
Posted 24 November 2014 - 04:04 AM
I made an api for this! http://www.computercraft.info/forums2/index.php?/topic/20602-autosaving-tables/page__p__195312#entry195312

</self promotion>

1. Please don't use "table" as a variable. It makes things go wrong.

2. Use local variables when you can

3. Why use

input = read()
newTable["username"] = input
instead of

newTable["username"] = read()
?

4. Indentation. Fix it. (If it's the forums derping, toggle editing mode into plaintext(upper left) )

5. When reading from a file, it's always nice to check if the file exists.

6. When overwriting a file, you don't need to delete it

7. None of the above account for the behavior you described.
Bomb Bloke #3
Posted 24 November 2014 - 04:15 AM
Currently the script will only call "save" if the password is changed - it doesn't bother if you change the username.
SNWLeader #4
Posted 26 November 2014 - 04:09 AM
Currently the script will only call "save" if the password is changed - it doesn't bother if you change the username.

Thank you, i have not been keeping check of this, but your solution did work.

I made an api for this! http://www.computercraft.info/forums2/index.php?/topic/20602-autosaving-tables/page__p__195312#entry195312 1. Please don't use "table" as a variable. It makes things go wrong. 2. Use local variables when you can 3. Why use
 input = read() newTable["username"] = input 
instead of
 newTable["username"] = read() 
? 4. Indentation. Fix it. (If it's the forums derping, toggle editing mode into plaintext(upper left) ) 5. When reading from a file, it's always nice to check if the file exists. 6. When overwriting a file, you don't need to delete it 7. None of the above account for the behavior you described.

I Thank you for your the suggestions.