Save and recover multiple data to/from a single file
Hi All
Is there a quick/tidy way of storing and recovering a small set of data/variables to/from a file? I think I've done it 'old school' by creating a string from the variables, saving the string, recovering the string then parseing the string to extract the variables.... but is there a better way...??
Say I have variables x1, x2, x3 and x4 (all numbers)... is there something like..
local file = io.open("/etc/plugboard/Variables","w+")
file:write(x1, x2, x3, x4)
then
file:read(x1, x2, x3, x4)
Any help greatly appreciated.....ta!
EJ
Not sure but try this?!
local q1 = 41.02
local q2 = 29.99
local q3 = 32.12
local q4 = 40000
f = io.open('/var/log/QUADvalue',"w+")
f:write(q1, q2, q3, q4)
f:close() <<<<< This works and data is output to file..
f = io.open('/var/log/QUADvalue',"r")
local n1, n2, n3, n4 = f:read("*number", "*number", "*number", "*number")
f:close()
f = io.open('/var/log/QUADvalue',"w+")
f:write(n4, n3, n2, n1)
f:close() <<<<< File is now empty... what've I done wrong?
Might help
Also. Just another thought. Are your values separated by a space when written to the file. If not I think they need to be
Garry
Brett may want to look the other way, but what about this?
local q1 = 41.00
local q2 = 29.99
local q3 = 32.12
local q4 = 40000
f = io.open('/var/log/QUADvalue',"w+")
f:write(q1.." "..q2.." "..q3.." "q4)
f:close() <<<<< This works and data is output to file..
f = io.open('/var/log/QUADvalue',"r")
local n1, n2, n3, n4 = f:read("*n", "*n", "*n", "*n")
f:close()
f = io.open('/var/log/QUADvalue',"w+")
f:write(n4, n3, n2, n1)
f:close() <<<<< File is now empty... what've I done wrong?
Iphone screwed up formatting but you get the idea!
Garry
I'm in pain watching this thread - how about this.
$ lua
Lua 5.1 Copyright (C) 1994-2006 Lua.org, PUC-Rio
> require("pl")
> t={q1=41,q2=29.99,q3=32.12,q4=4000}
> file.write("/tmp/test.cfg",pretty.write(t))
>
$ cat /tmp/test.cfg
{
q3 = 32.12,
q2 = 29.99,
q1 = 41,
q4 = 4000
}
$ lua
Lua 5.1 Copyright (C) 1994-2006 Lua.org, PUC-Rio
> require("pl")
> t=pretty.read(file.read("/tmp/test.cfg"))
> print(t.q3)
32.12
>
$
You know you've got the bug bad when other peoples dodgy coding creates physical pain. ;)
Anyhow thanks for this I can use it myself,
I did read the penlight manual however I find a lot of these things are written by coders for coders and are difficult for noncoders like myself to understand.
Simple examples like the one you had provided clears matters up greatly and actually makes me wonder what I had a problem with in the first place!
thanks again, the painkillers are in the post!
I had a bit of a hack and slash at your code to tidy it up a little - I tried not to change the functionality as it was implmented. You should be able to undestand the changes I made. Its cleans up the code to make it easier to work on. As I can't test what I've done you should be able to take this forward.
Brett
Attachment | Size |
---|---|
CConsBetaApplet.lua | 7.06 KB |
Lots of folks that I talk to about the HAH are put off by the perceived 'complexity' of scripting. So, anything that can explain (with notes, example code and a few photos) how to perform a specific function can only help.
Derek.
Use the penlight config module.
In the plugboard document I say you should read the PENLIGHT manual.
http://www.dbzoo.com/livebox/hah_plugboard_v2#standard_libraries
There is a good reason for this :)
http://stevedonovan.github.com/Penlight/api/modules/pl.config.html
Brett