lua-users home
lua-l archive

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



> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Philippe Lhoste
> Sent: donderdag 27 juni 2013 10:50
> To: lua-l@lists.lua.org
> 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
> --  --  --  --  --  --  --  --  --  --  --  --  --  --
>