Example of database:
lastX: 516 lastY: -214 lastZ: 72
This is less required, but is there any easy algorithms for basic encryption and decryption?
Posted 12 April 2013 - 06:01 AM
lastX: 516 lastY: -214 lastZ: 72
Posted 12 April 2013 - 06:19 AM
Posted 12 April 2013 - 06:23 AM
TheCoryKid, on 12 April 2013 - 06:19 AM, said:
Posted 12 April 2013 - 06:31 AM
Signify, on 12 April 2013 - 06:23 AM, said:
TheCoryKid, on 12 April 2013 - 06:19 AM, said:
file = fs.open("database", "r")
while true do
line = fs.readLine()
if string.sub(line, 1, string.find(line, ":")) == "lastZ" then
lastZ = string.sub(line.."", string.find(line, ":") + 1)
end
end
The code might not work, but it's the general idea. Read the Lua documentation to learn more about string.sub() and string.find().
Posted 12 April 2013 - 06:34 AM
--saving
tabletosavetofile={lastX=516,lastY=-214,lastZ=72}
file.write(textutils.serialize(tabletosavetofile))
--loading
tablereadfromfile=textutils.unserialize(file.readAll())
it would be WAAY easier
Posted 12 April 2013 - 06:36 AM
PixelToast, on 12 April 2013 - 06:34 AM, said:
--saving
tabletosavetofile={lastX=516,lastY=-214,lastZ=72}
file.write(textutils.serialize(tabletosavetofile))
--loading
tablereadfromfile=textutils.unserialize(file.readAll())
it would be WAAY easierPosted 12 April 2013 - 06:36 AM
local function saveSettings(s,l) -- s is the table with your information, l is the filename
local file = fs.open(l, "w") -- this opens a new file if exists overwrites it (look into the FS Api in the Wiki)
if file then
for i=1, #s do -- start a loop from one to number of entries in table s
file.writeLine(s[i]) write it to the file (FS Api again)
end
file.close() -- close the file is very important
end
end
local function readSettings(l,s) -- l is the line you want to read s is the filename as string
local file = fs.open(l, "r") -- open file in read mode
if file then
log = {} -- create table for your file
local line = file.readLine() -- set variable
while line do
table.insert(log, line) -- add the lines from file to the table
line = file.readLine() --check the next line
end
file.close() -- close the file
return log[l] --return the line you need
end
end
local lastpos = {516,-214,72} -- make sure x y z values are always in the same position of the table
saveSettings(lastpos,"lastcoords")
lastZ = readSettings(3,"lastcoords") -- read line 3 cause it contains the Z data
--or use it as function
local function getLastP()
lastX = readSettings(1,"lastcoords") -- read line 3 cause it contains the Z data
lastY = readSettings(2,"lastcoords") -- read line 3 cause it contains the Z data
lastZ = readSettings(3,"lastcoords") -- read line 3 cause it contains the Z data
end
Posted 12 April 2013 - 06:37 AM
Posted 12 April 2013 - 06:40 AM
function readPrefs(name,tf) -- reads data from a file local file=fs.open(name,"r") if not file then return false end local dat if tf then dat=file.readAll() else dat=textutils.unserialize(file.readAll()) end file.close() return dat endfrom my api
0 members, 2 guests, 0 anonymous users