83 posts
Location
Being the stereotypical kid in forums.
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
3057 posts
Location
United States of America
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()
83 posts
Location
Being the stereotypical kid in forums.
Posted 23 March 2016 - 01:47 PM
Doesn't work..
Doesnt delete the variable
83 posts
Location
Being the stereotypical kid in forums.
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
41 posts
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
83 posts
Location
Being the stereotypical kid in forums.
Posted 24 March 2016 - 05:01 PM
So I do Lyqyd = {"blabl","bla","bla"} Lyqyd
in a line instead of
Lyqyd = {"blabl","bla","bla"}?
3057 posts
Location
United States of America
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.
8543 posts
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.
41 posts
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
2427 posts
Location
UK
Posted 25 March 2016 - 10:11 AM
or you could add a blank line to the end of your file
463 posts
Location
Star Wars
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
8543 posts
Posted 25 March 2016 - 02:28 PM
I did write
an API to work with files like these.
83 posts
Location
Being the stereotypical kid in forums.
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