[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Size of a non numerical indexed table
- From: Mike Pall <mikelu-0509@...>
- Date: Thu, 1 Sep 2005 15:43:26 +0200
Hi,
Lisa Parratt wrote:
> Which is all well and good, but it simply didn't *work*, which is why I
> had to switch to the old method.
It worked -- but not in the way you expected it to.
The semantics have changed.
> Unfortunately, I'm currently too busy to track down an example :(
This one will print 3 for pre-51w6 and 0 for 51w6:
local function foo(...)
print(table.getn(arg))
end
foo(nil, 1, nil)
OTOH the following will always print 3:
local function foo(...)
print(select("#", ...))
end
foo(nil, 1, nil)
I.e. convert to select("#", ...) if you want to know the true
number of args in Lua 5.1.
And BTW ... ipairs() always stopped at the first nil. This
behaviour is consistent with Lua 5.0 semantics.
Here is the proper way to iterate over all varargs in Lua 5.1:
local function foo(...)
for i=1,select("#", ...) do
print(i, (select(i, ...))) -- The parentheses are deliberate.
end
end
foo(nil, 1, nil)
Putting the varargs into a table with {...} is slow. Don't
use this unless you really need them to be in a table.
Bye,
Mike