lua-users home
lua-l archive

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


HyperHacker <hyperhacker <at> gmail.com> writes:

> 
> On Thu, Dec 8, 2011 at 20:37, PROXiCiDE <saiyuk7 <at> gmail.com> wrote:
> > Xavier Wang <weasley.wx <at> gmail.com> writes:
> >
> >>
> >> Maybe you can do something like this:
> >>
> >> function Test(s, a, ...)
> >>   -- do something to calculate the first field
> >>   local ret = --....
> >>   return ret, Test(s, ...)
> >> end
> >>
> >> ---------- Forwarded message ----------
> >> From: PROXiCiDE <saiyuk7 <at> gmail.com>
> >> Date: 2011/12/9
> >> Subject: Returning a ellipses
> >> To: lua-l <at> lists.lua.org
> >>
> >> I want to pass a function that supports
> >> ellipses "..." and return several of
> >> them into different variables..
> >>
> >> example
> >>
> >> function Test(...)
> >> end
> >>
> >> a,b,c,d,e = Test(1,2,3,4,5)
> >>
> >> Reason for this being, i want to be able to
> >> read a structured data into variables...
> >>
> >> example reason
> >>
> >> magic,version, headersize,directorycount,directorylocation =
> >> ReadDirectory(f,"s",16,32,32,32)
> >>
> >> S = obviously a String
> >> 16 = Int16
> >> 32 = Int32
> >>
> >> I would actually parse the the ellipses using the
> >> "var = select(i,...)" from a "for" statement
> >>
> >> But what i cant understand is how do i seperate
> >> these into different variables
> >> that are returned
> >>
> >>
> >
> >
> > Well Test will be reading data from a binary file
> > into these specific variables, the ellipses
> > would hold the size of the data to be read from the file
> >
> > example
> >
> > function Test(f,...)
> >  for i=1,select("#",...) do
> >  local v = select(i,...)
> >  if (type(v) == "number") then
> >  store_var = FileReadSize(f,v)
> >  end
> >  return -- Store Variables here
> > end
> >
> > a,b,c = Test(f,32,16,32)
> >
> >
> >
> 
> So you want to store your return values in a table, and unpack them at the end.
> function Test(f,...)
>  local ret = {}
>  for i=1,select("#",...) do
>  local v = select(i,...)
>  if (type(v) == "number") then
>  ret[#ret+1] = FileReadSize(f,v)
>  end
>  return unpack(ret)
> end
> 


thank you very much this indeed worked, i wish there was
a simplier way, in C you could read a file structure with
fread function

sorry to trouble you, would there be a way to simplify this?
like 

array = Test({magic=16,header=32,version=32})
array.magic 
array.header
array.version

created from reading the file contents? ive tried

function Test(arg)
 local t={}
 for k,v in pairs(arg) do
  t[v] = FileReadSize(f,v)
 end
 return t
end

but the ordered of how the structured was made is
sorted in a different way and it becomes unorganized..

example.. array.magic does not read it in the correct
order as it was created in the index of the argument
passed in the function

instead array.magic becomes array.header