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

Deleting external variable (fs/io)

Started by X3ME, 22 March 2016 - 09:06 PM
X3ME #1
Posted 22 March 2016 - 10:06 PM
I have a file called database wich contains something like this:
X3ME03 = {"X3ME","2","5425"}
Lyqyd = {"Lyqyd","78","35423"}

How do I delete variable Lyqyd for example?

Thanks! ;)/>
Edited on 23 March 2016 - 12:35 PM
KingofGamesYami #2
Posted 22 March 2016 - 10:36 PM
1) read contents of file
2) modify string
3) write to file

EG:

local file = fs.open( "database", "r" )
local data = file.readAll()
file.close()
file = fs.open( "database", "w" )
file.write( data:gsub( "Lyqyd.-\n" ) )
file.close()
X3ME #3
Posted 23 March 2016 - 01:47 PM
Doesn't work..

Doesnt delete the variable
X3ME #4
Posted 23 March 2016 - 02:08 PM
I tried making the program "test" that incorporated that:
local file = fs.open( "database", "r" )
local data = file.readAll()
file.close()
file = fs.open( "database", "w" )
file.write( data:gsub( "Lyqyd.-\n" ) )
file.close()
and had the file "database" with the following:

Lyqyd = {"Lyqyd","3","436"}

did : test
No errors but nothing happend to the database file either
moTechPlz #5
Posted 23 March 2016 - 03:52 PM
For this code to work you need a trailing return in your database file.(maybe two) So, a return after your last data entry. It should delete any data before just not at the end because it ends in a eof and not in a newline.

And probably gsub wants another argument

file.write( data:gsub( "Lyqyd.-\n", "" ) )
Edited on 23 March 2016 - 03:48 PM
X3ME #6
Posted 24 March 2016 - 05:01 PM
So I do Lyqyd = {"blabl","bla","bla"} Lyqyd
in a line instead of
Lyqyd = {"blabl","bla","bla"}?
KingofGamesYami #7
Posted 24 March 2016 - 05:03 PM
The easiest way to make a database is to literally put all the data in a table, serialize the table, and write that to a file. Any other way just generates more work.

The only thing I'd recommend, is copying textutils.serialize from an earlier version. Around 1.6 it changed to be readable, and that bloats file sizes.
Lyqyd #8
Posted 24 March 2016 - 07:12 PM
I'd recommend against using the old serialization function. The new one understands natural indexes, which saves a good few characters. If your data won't contain multiple consecutive spaces or line breaks, you can use a simple gsub call on the serialized table to get a string that's smaller than either the old or the new would have generated on their own.
moTechPlz #9
Posted 24 March 2016 - 07:48 PM
@ X3ME

No look, gsub looks for a string to replace. The search parameter used is "Lyqyd.-\n". And this is bit of a code where "\n" means 'a newline' and ".-" means 'anything in between'. So gsub looks for a string starting with "Lyqyd" and ending in a "newline" with anything in between. Now, if the database file last line does not end on a "newline" but a "End Of File" or EOF short it is not recognized and not removed.

If you put Lyqyd entry first it should get deleted.

Lyqyd = {"Lyqyd","78","35423"}
X3ME03 = {"X3ME","2","5425"}
Edited on 24 March 2016 - 06:56 PM
Lupus590 #10
Posted 25 March 2016 - 10:11 AM
or you could add a blank line to the end of your file
Sewbacca #11
Posted 25 March 2016 - 12:42 PM

os.loadAPI( "database")
database.Lyqyd = nil

file = io.open( "database", "w" )
for name, tab in ipairs(database) do
  file:write( name .. " = " .. textutils.serialize(tab) .. "\n" ) )
end
file:close()

Note, if the type of tab isn't a table, then you will get errors and if a string in tab contains a \n then you will get also errors.
Edited on 25 March 2016 - 11:42 AM
Lyqyd #12
Posted 25 March 2016 - 02:28 PM
I did write an API to work with files like these.
X3ME #13
Posted 26 March 2016 - 07:20 PM
I'll look into Lyqyd's API and you guy's suggestions, thank you so much.
Regarding CC problems I have been having I came to teh conclusion Tekkit Lengends Modpack is uncompatible with certain events in CC