lua-users home
lua-l archive

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

What might be nice is an optional argument to unpack that replaces #
if it is present. Also table.concat and perhaps other functions that
rely on # rather than stopping at the first nil. So this sort of thing
would be possible:

t = { "A", "B", "C", "D", "E", "F", "G", "H" } -- pretend this table
is being recyled
-- this time we only want to concatenate three elements:

t[1] = "U"
t[2] = "V"
t[3] = "W"
t[4] = nil -- may not be necessary with the second argument to table.concat

print(table.concat(t, 3)) -- use 3 instead of #t


At the moment table.concat(t) uses #t (which is still 8) and gives an
error when it meets the nil at t[4]. As far as I can tell, this change
would be easy to implement, and would soften the occasional
unpredictability of the # operator. It could also be used with unpack,
padding with nils as necessary.


2009/6/24 Bulat Ziganshin <>:
> Hello Bulat,
> Wednesday, June 24, 2009, 2:02:56 AM, you wrote:
>>> if you want to iterate over the varargs of a function, when some of them
>>> may be nil, you have to use:
>> afai now realized, it's neither guaranteed with #:
> oh, sorry, it should work with select.
> it's just me who need #arr to compute number of all elements including nils
> --
> Best regards,
>  Bulat