Any way to save a table into a file??

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Any way to save a table into a file??

berrdolagas
I'm trying to save a table like:

exp={"Eggs","Rice","Bread"}
On a file calling "Table.dat" on the VFS card. I tried to use pack
("S",exp) but the function only returns the first element of the
table, "Eggs"! How do i save a table to a file?






 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply | Threaded
Open this post in threaded view
|

RE: Any way to save a table into a file??

Emil Tchekov-3
Hi,

> exp={"Eggs","Rice","Bread"}

The correct way (in your example) to save all the
contents is:

data=pack("SSS", exp)

otherwise (with only one "S") - pack proceeds only the
first element of the table - exactly what you get.

Regards

Emil Tchekov


       

       
               
___________________________________________________________
Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply | Threaded
Open this post in threaded view
|

Re: Any way to save a table into a file??

berrdolagas
And if the data is:

bread={"White","Linaza"}
breakfast={"Eggs","Rice","Orange juice"}
meal={"Roast beef","Spaghetti","Watermelon"}
dinner={bread,"Milk"}

tobuy={breakfast,meal,dinner}

If i want to pack table tobuy?

Thanks.





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply | Threaded
Open this post in threaded view
|

Re: Re: Any way to save a table into a file??

David McNab
berrdolagas wrote:
> bread={"White","Linaza"}
> breakfast={"Eggs","Rice","Orange juice"}
> meal={"Roast beef","Spaghetti","Watermelon"}
> dinner={bread,"Milk"}
>
> tobuy={breakfast,meal,dinner}
>
> If i want to pack table tobuy?

So easy in python - it's just:

  import pickle
  pickle.dump(tobuy, file("tobuy.dat", "w"))
and to get it back again:
  tobuy = pickle.load(file("tobuy.dat"))

For lua, there might be some third-party table serialisation libraries
available. Otherwise you could readily cook up your own one which
iterates through the index,val pairs, testing the val type, writing out
a prefix char to indicate this type, then the value, with a recursion
for table values, kinda like:

function dumpToFile(obj, f)
   typ = type(obj)
   if t == 'number' then
      -- write out 'n' for number
      -- write out obj as a number
   elseif t == 'table' then
      -- write out 't' for table
      -- write out number of index,value pairs
      for k,v in table do
        -- write out 'k' meaning 'table key'
        -- write out table val
        dumpToFile(v, f) -- recursion
      end
   elseif t == 'string' then
      -- write out string
   elseif yada yada ...
     ...
   end
end

Userdata and functions would be a problem though.

Cheers
David


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply | Threaded
Open this post in threaded view
|

Re: Re: Any way to save a table into a file??

David McNab
David McNab wrote:
> For lua, there might be some third-party table serialisation libraries
> available. Otherwise you could readily cook up your own one which
> iterates through the index,val pairs, testing the val type, writing out
> a prefix char to indicate this type, then the value, with a recursion
> for table values, kinda like:
>
> function dumpToFile(obj, f)

Dammit - forgot to give a warning - such an algo will crash with a stack
overflow if any circle of references exists in the objects being
serialised. For instance, a table A which contains table B, which
contains table C, which contains table A.

If wanting to serialise out straight trees like your example, the algo
would work fine, otherwise you'd need an algo which builds a checklist
of refs, and also dumps out the tables with an ID - on reading back, one
can then add the refs off the checklist, and all will be fine - cyclic
references won't be a problem.

Cheers
David




 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/