lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>  Fri, 12 Mar 2004 15:28:35 +0100
>  Philippe Lhoste <PhiLho@GMX.net> wrote:
>
>  JK wrote:
>  > I found this question with many answers regarding the language Ruby. /*
>  > After I do bunch of computations and create a huge array with bunch of
>  > numbers, I like to use the already-computed array in another program
>  > rather than re-computing it each time I run the program. How can I
>  > save the array object in one program and read it from another program
>  > in Ruby?
>  > */
>  > and I found this answer the simplest one:
>  > 
>  > You can use marshalling:
>  > 
>  > array = ...
>  > File.open("array", "wb") do |f|
>  >   Marshal.dump(array, f)
>  > end
>  > 
>  > 
>  > To reload:
>  > 
>  > array = nil
>  > File.open("array", "rb") do |f|
>  >   array = Marshal.load(f)
>  > end
>  > 
>  > Now, I need the same thing in Lua. Is it possibile to do this elegantly 
>  > in just 8 lines in Lua?
>  
>  I can do that in 1 line only, but it will not be very readable...
>  A number of methods to serialize tables have been given here over the 
>  years, none perfect because Lua can store all kind of strange data 
>  (userdata for example) as keys or values.
>  
>  Since you have very simple needs (manage a simple array of numbers 
>  only), I can give a very simple code:
>  
>  local arrayFile = "Array.txt"
>  local array = { 1, 5, 12.3, 0.25, -45.01, 7 }
>  local f = io.open(arrayFile, "w")
>  f:write("return {\n")
>  for i, v in ipairs(array) do
>     f:write("[" .. i .. "]=" .. v .. ",") -- Add \n if you want the file 
>  to be readable by humans
>  end
>  f:write("}\n")
>  f:close()
>  
>  And read back the values with:
>  
>  local array = dofile(arrayFile)


Hi,

This becomes interesting for me:
when I read with dofile, the array data several times  are in main memory:

1) when I read the chunk (this could be modified, though)
2) in the bytecode
3) in the table.
If, additionally, the arrays in fact are userdata objects in order to pass
them to C, we have one more time...

Or am I wrong ?

The solution I implemented is something like

a=CreateArray(100000)
data(a)
$
1
2
...
100000
$

where  the $'s are chunk separators which might be replaces by XML tags in
a future version.

The data statement triggers the parser for the next chunk, it could
be even fread.

Another solution would just using Lua for indexing a binary file.

In the  baseline, this  is a marshalling  solution like in  ruby.  Not
very elegant, but it works. Of course I would like some solution which
is more lua'ish.

Juergen