lua-users home
lua-l archive

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


On Thursday 23 February 2006 20:01, PA wrote:
[...]
> Ok... so... for the sake of completeness... how would you capture the
> length of a table with trailing nil in plain Lua 5.1?
>
> function test()
>          return 1, nil, 2, nil
> end
>
> local someResults = { test() }

You can't, not like that, because tableifying it like that loses the necessary 
information... I'd be inclined to try:

function tableify(...)
	return arg
end

local someResults = tableify(test())
print("n", table.getn(someResults))

This works (if it *does* work, it's untested) because of the horrible 
backwards-compatibility feature where a table entry called 'n' preempts the 
actual size of the array. It might be safer to copy arg.n into something else 
inside tableify(), because there are all kinds of weird edge conditions with 
n that make it not behave the way you expect. In which case, you'd have to do 
someResults.othern instead of table.getn(someResults).

You may be able to tell that I'm a great fan of using functions for 
Haskell-like pattern matching...

-- 
+- David Given --McQ-+ 
|  dg@cowlark.com    | "Anything that makes people that angry is worth
| (dg@tao-group.com) | doing again." --- Scott Adams
+- www.cowlark.com --+ 

Attachment: pgp1WKP7fZBjt.pgp
Description: PGP signature