Save and recover multiple data to/from a single file

13 replies [Last post]
EJ-Ambient
Offline
Ringwood, United Kingdom
Joined: 5 Aug 2011

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

brett
Offline
Providence, United States
Joined: 9 Jan 2010
Use the penlight config

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

EJ-Ambient
Offline
Ringwood, United Kingdom
Joined: 5 Aug 2011
Thanks for that...

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

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Mmm

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?

 

EJ-Ambient
Offline
Ringwood, United Kingdom
Joined: 5 Aug 2011
Nope!

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

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
What text is on line 33?

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

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Blind leading the blind

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

brett
Offline
Providence, United States
Joined: 9 Jan 2010
I'm in pain watching this

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

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Ha ha. Thanks

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!

EJ-Ambient
Offline
Ringwood, United Kingdom
Joined: 5 Aug 2011
Many Thanks..

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

PS Solpadol does the trick for me!

EJ-Ambient
Offline
Ringwood, United Kingdom
Joined: 5 Aug 2011
Hand holding, please

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

AttachmentSize
README.zip 6.29 KB
brett
Offline
Providence, United States
Joined: 9 Jan 2010
I had a bit of a hack and

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

AttachmentSize
CConsBetaApplet.lua 7.06 KB
EJ-Ambient
Offline
Ringwood, United Kingdom
Joined: 5 Aug 2011
Well hacked...

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

derek
Offline
Glasgow, United Kingdom
Joined: 26 Oct 2009
Great potential for a 'case study'

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.

Hardware Info