write a list of variables to a file, formatted

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

write a list of variables to a file, formatted

Daniel Barna
Hi, I have a function which return a list of variables:
function some_function()
  return a,b,c,d;
end

I would like to write this to a file, using a whitespace as the separator, something like this:
some_file:write(some_function());

How can I do this?
Thank you
Daniel

Reply | Threaded
Open this post in threaded view
|

Re: write a list of variables to a file, formatted

Philippe Lhoste
On 26/06/2013 16:45, Daniel Barna wrote:
> Hi, I have a function which return a list of variables:
> function some_function()
>    return a,b,c,d;
> end
>
> I would like to write this to a file, using a whitespace as the separator, something like
> this:
> some_file:write(some_function());

 > lua52
Lua 5.2.0  Copyright (C) 1994-2011 Lua.org, PUC-Rio
 > f = function () return 1, 2, 3, 4 end
 > print(f())
1       2       3       4
 > print(table.concat(table.pack(f()), " "))
1 2 3 4

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --


Reply | Threaded
Open this post in threaded view
|

Re: write a list of variables to a file, formatted

Scott Morgan
In reply to this post by Daniel Barna
On 26/06/13 15:45, Daniel Barna wrote:

> Hi, I have a function which return a list of variables:
> function some_function()
>   return a,b,c,d;
> end
>
> I would like to write this to a file, using a whitespace as the
> separator, something like this:
> some_file:write(some_function());
>
> How can I do this?

Quick and easy:

file:write(table.concat({some_func()}, " "))

Scott


Reply | Threaded
Open this post in threaded view
|

Re: write a list of variables to a file, formatted

Javier Guerra Giraldez
In reply to this post by Daniel Barna
On Wed, Jun 26, 2013 at 9:45 AM, Daniel Barna <[hidden email]> wrote:
> Hi, I have a function which return a list of variables:
> function some_function()
>   return a,b,c,d;
> end


nitpicking: that function doesn't return a list of variables.  it
returns a list of values.

the values don't have names, so if you want those in your file, you're
out of luck unless you return those too.

why not return a table?  then you can write both the keys and values
to the file.

--
Javier

Reply | Threaded
Open this post in threaded view
|

Re: write a list of variables to a file, formatted

Daniel Barna
Hi,
Thanks for the answers. Yes I meant a list of values, I don't care the names. 
concatenating them as suggested seems to be the best solution (print probably writes them separated by spaces, indeed, but I wanted to use file:write)
Thanks
D


On Wed, Jun 26, 2013 at 5:00 PM, Javier Guerra Giraldez <[hidden email]> wrote:
On Wed, Jun 26, 2013 at 9:45 AM, Daniel Barna <[hidden email]> wrote:
> Hi, I have a function which return a list of variables:
> function some_function()
>   return a,b,c,d;
> end


nitpicking: that function doesn't return a list of variables.  it
returns a list of values.

the values don't have names, so if you want those in your file, you're
out of luck unless you return those too.

why not return a table?  then you can write both the keys and values
to the file.

--
Javier


Reply | Threaded
Open this post in threaded view
|

RE: write a list of variables to a file, formatted

Thijs Schreijer
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On
> Behalf Of Daniel Barna
> Sent: woensdag 26 juni 2013 17:13
> To: Lua mailing list
> Subject: Re: write a list of variables to a file, formatted
>
> Hi,
> Thanks for the answers. Yes I meant a list of values, I don't care the
> names.
> concatenating them as suggested seems to be the best solution (print
> probably writes them separated by spaces, indeed, but I wanted to use
> file:write)
> Thanks
> D
>

Just a remark on the proposed solutions; be careful with nil values returned, the sequences might not be valid, and it won't return anything after the nil value. If you expect nils, test thoroughly...

Thijs


Reply | Threaded
Open this post in threaded view
|

Re: write a list of variables to a file, formatted

Eike Decker
In reply to this post by Daniel Barna


Am 26.06.2013 17:06 schrieb "Daniel Barna" <[hidden email]>:
>
> Hi, I have a function which return a list of variables:
> function some_function()
>   return a,b,c,d;
> end
>
> I would like to write this to a file, using a whitespace as the separator, something like this:
> some_file:write(some_function());
>
> How can I do this?
> Thank you
> Daniel
>

How about this:

function concat(sep,...)

local t = {}

for i=1,select('#',...) do

t[i] = tostring(select(i,...))

end

return table.concat(t,sep)

end


function test() return 1,nil,2,nil,nil end

print(concat(", ",test()))

---

output:
1, nil, 2, nil, nil

As pointed out before by Thijs Schreijer, wrapping arguments into tables causes trouble when nil values are around. Besides, nonstring values can also be problematic with concat. I am not sure, but the code I wrote should handle both cases correctly.

Cheers,
Eike


Reply | Threaded
Open this post in threaded view
|

Re: write a list of variables to a file, formatted

Philippe Lhoste
On 26/06/2013 21:53, Eike Decker wrote:
> As pointed out before by Thijs Schreijer, wrapping arguments into tables causes trouble
> when nil values are around. Besides, nonstring values can also be problematic with concat.
> I am not sure, but the code I wrote should handle both cases correctly.

Lua 5.2.0  Copyright (C) 1994-2011 Lua.org, PUC-Rio
 > f = function () return 1, 2, nil, 3, 4 end
 > print(f())
1       2       nil     3       4
 > print(table.concat(table.pack(f()), " "))
stdin:1: invalid value (nil) at index 3 in table for 'concat'
stack traceback:
         [C]: in function 'concat'
         stdin:1: in main chunk
         [C]: in ?
 > print(table.concat({ f() }, " "))
stdin:1: invalid value (nil) at index 3 in table for 'concat'
stack traceback:
         [C]: in function 'concat'
         stdin:1: in main chunk
         [C]: in ?

Non-string values are fine, but nil is indeed a showstopper...
Your wrapper, Eike, takes care nicely of the problem (and nil can be ignored or changed to
something specific, too).

Note that print() doesn't separate with spaces but with tabs.

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --


Reply | Threaded
Open this post in threaded view
|

RE: write a list of variables to a file, formatted

Thijs Schreijer
In reply to this post by Eike Decker

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On
> Behalf Of Eike Decker
> Sent: woensdag 26 juni 2013 21:54
> To: Lua mailing list
> Subject: Re: write a list of variables to a file, formatted
>
>
> Am 26.06.2013 17:06 schrieb "Daniel Barna" <[hidden email]>:
> >
> > Hi, I have a function which return a list of variables:
> > function some_function()
> >   return a,b,c,d;
> > end
> >
> > I would like to write this to a file, using a whitespace as the
> separator, something like this:
> > some_file:write(some_function());
> >
> > How can I do this?
> > Thank you
> > Daniel
> >
> How about this:
> function concat(sep,...)
> local t = {}
> for i=1,select('#',...) do
> t[i] = tostring(select(i,...))
> end
> return table.concat(t,sep)
> end
>
> function test() return 1,nil,2,nil,nil end
> print(concat(", ",test()))


-- use from 5.2 or define for 5.1
local pack = table.pack or function(...) return {n = select("#", ...), ...} end

function concat(sep,...)
        local t = pack(...)
        for i=1, t.n do
                t[i] = tostring(t[i])
        end
        return table.concat(t, sep)
end

Nitpicking, but this is slightly faster, probably because of lesser use of varargs (...) (see test below)
Thijs

>
> As pointed out before by Thijs Schreijer, wrapping arguments into tables
> causes trouble when nil values are around. Besides, nonstring values can
> also be problematic with concat. I am not sure, but the code I wrote
> should handle both cases correctly.
>
> Cheers,
> Eike

Timed test:
=============
local time = require('socket').gettime

-- use from 5.2 or define for 5.1
local pack = table.pack or function(...) return {n = select("#", ...), ...} end

function concat1(sep,...)
        local t = pack(...)
        for i=1, t.n do
                t[i] = tostring(t[i])
        end
        return table.concat(t, sep)
end

function concat2(sep,...)
  local t = {}
  for i=1,select('#',...) do
  t[i] = tostring(select(i,...))
  end
  return table.concat(t,sep)
end

function test() return 1,nil,2,nil,nil end
function exec(f,n)
  for i = 1, n do local x = f(", ",test()) end
end

function runner(n)
  local t1 = time()
  exec(concat1,n)
  local t2 = time()
  exec(concat2,n)
  local t3 = time()

  print(n.." iterations")
  print("   concat1: ", t2-t1)
  print("   concat2: ", t3-t2)
  print("   diff: ", ((t3-t2)/(t2-t1) - 1) * 100, "% slower")
end
runner(1000)
runner(10000)
runner(100000)
runner(1000000)

Results:
1000 iterations
   concat1: 0
   concat2: 0
   diff: -1.#IND % slower
10000 iterations
   concat1: 0.046800136566162
   concat2: 0.062400102615356
   diff: 33.333163520026 % slower
100000 iterations
   concat1: 0.4212007522583
   concat2: 0.59280109405518
   diff: 40.740749126593 % slower
1000000 iterations
   concat1: 6.9420120716095
   concat2: 8.7672154903412
   diff: 26.292138358505 % slower
Program completed in 16.91 seconds (pid: 8024).


Reply | Threaded
Open this post in threaded view
|

RE: write a list of variables to a file, formatted

Thijs Schreijer
In reply to this post by Philippe Lhoste


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On
> Behalf Of Philippe Lhoste
> Sent: donderdag 27 juni 2013 10:50
> To: [hidden email]
> Subject: Re: write a list of variables to a file, formatted
>
> On 26/06/2013 21:53, Eike Decker wrote:
> > As pointed out before by Thijs Schreijer, wrapping arguments into tables
> causes trouble
> > when nil values are around. Besides, nonstring values can also be
> problematic with concat.
> > I am not sure, but the code I wrote should handle both cases correctly.
>
> Lua 5.2.0  Copyright (C) 1994-2011 Lua.org, PUC-Rio
>  > f = function () return 1, 2, nil, 3, 4 end
>  > print(f())
> 1       2       nil     3       4
>  > print(table.concat(table.pack(f()), " "))
> stdin:1: invalid value (nil) at index 3 in table for 'concat'
> stack traceback:
>          [C]: in function 'concat'
>          stdin:1: in main chunk
>          [C]: in ?
>  > print(table.concat({ f() }, " "))
> stdin:1: invalid value (nil) at index 3 in table for 'concat'
> stack traceback:
>          [C]: in function 'concat'
>          stdin:1: in main chunk
>          [C]: in ?
>
> Non-string values are fine, but nil is indeed a showstopper...

Nil breaking up a sequence can be overcome, but then I also ran into this with table.concat(). I tried this one liner;
   table.concat({...}, ", ", 1, select("#", ...))
and though table.concat() coerces the numbers nicely, it doesn't coerce nils to strings. So no way around iterating over it, or is there?

Thijs


> Your wrapper, Eike, takes care nicely of the problem (and nil can be
> ignored or changed to
> something specific, too).
>
> Note that print() doesn't separate with spaces but with tabs.
>
> --
> Philippe Lhoste
> --  (near) Paris -- France
> --  http://Phi.Lho.free.fr
> --  --  --  --  --  --  --  --  --  --  --  --  --  --
>