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

Member for

15 years 3 months

EJ-Ambient

14 years 5 months ago

however.... here's my snippet..

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("*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?

Thanks..

EJ

Member for

15 years 7 months

garrydwilms

14 years 5 months ago

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?

 

Member for

15 years 3 months

EJ-Ambient

14 years 5 months ago

gives the same error....

lua: /etc/plugboard/QuadApplet.lua:33: bad argument #1 to 'write' (string expected, got nil)
stack traceback:
    [C]: in function 'write'

It looks like the f:read construct is wrong/not entered correctly!!!

I'll try anything.....

EJ

Member for

15 years 7 months

garrydwilms

14 years 5 months ago

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

Member for

15 years 7 months

garrydwilms

14 years 5 months ago

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

Member for

16 years 10 months

brett

14 years 5 months ago

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
>
$

Member for

15 years 7 months

garrydwilms

14 years 5 months ago

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!

Member for

15 years 3 months

EJ-Ambient

14 years 5 months ago

ditto to what Garry said ..... and thanks for a neat example...... EJ

PS Solpadol does the trick for me!

Member for

15 years 3 months

EJ-Ambient

14 years 5 months ago

Hi Brett

I'm sending you a zip of a project I'm working on....can you cast an eye over it please (there's an error in it!)...

It's to enable people with PV arrays that merge with the mains reading to get a 'clean' house use figure.  I've got a couple of people helping with it and I think I've cracked it.....but can you look at the code (and help with the error) and critique it, please....ta!

I've put more info in the readme.txt file....

regards

EJ

Attachments
README.zip (6.29 KB)

Member for

16 years 10 months

brett

14 years 5 months ago

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

Attachments

Member for

15 years 3 months

EJ-Ambient

14 years 5 months ago

Beautifully done - it's nice to see my code 'as it should be wrote'!   Maybe I can do better in future now that I've got a great example to work from....thanks muchly!.....

Do you think (after I've tweaked the notes and tested it on a live system) that its publishable?

Thanks again for all your efforts!

EJ

Member for

17 years

derek

14 years 5 months ago

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.