lua-users home
lua-l archive

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


2009/6/26 Vaughan McAlley <ockegheim@gmail.com>:
> 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

Why would you want to recycle a table in the first place without
clearing it first ? Assuming you want to avoid table creation because
you imagine it's slow, did you benchmark it before asking for a Lua
API change ?

> -- 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

Alternatively you can do:

    print(table.concat({unpack(t, 1, 3)}))

But this creates a new table, which for some reason you wanted to avoid.