[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lua tables/keys
- From: "Eric Tetz" <erictetz@...>
- Date: Fri, 14 Mar 2008 11:27:19 -0700
On Wed, Mar 12, 2008 at 8:16 AM, Eva Schmidt <es@sol-3.de> wrote:
> I think your problem is, that your table is actually empty: If you
> don't assign any value to your tables keys, their value is simlpy nil.
His table is only empty if the initializers (a,b,c,d) are nil. If they
aren't, the table contains (up to) 4 elements, with keys 1 through 4,
and value a through b. See Patrick's post.
> Try out
> a = {a = 5,b,c,d}
That's equivalent to:
a = { ["a"] = 5, [1] = b, [2] = c, [3] = d }
i.e. a is 'stringized' and used as a key name, while b through d are
variables who values are used to initialize array elements.
Anyway, when I want to create a set like this, I just invert an array.
I have an invert function in my utility anyway:
function table.invert( tbl )
local rv = {}
for key,val in pairs( tbl ) do rv[ val ] = key end
return rv
end
So I just say:
a = table.invert{ a, b, c, d }
Cheers,
Eric