lua-users home
lua-l archive

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


2008/9/21 Geoff Leyland <geoff_leyland@fastmail.fm>:
> On 21/09/2008, at 3:50 PM, Mark Meijer wrote:
>
>> AFAIK, any keys present in a table constructor have to be immediate
>> values. At any other place, you can use any expression you like to
>> access tables (aside from some exceptions, such as nil, or keywords or
>> symbols as names when using the dot notation).
>
> I don't understand the original question either, but is it relevant that
> this:
>
> a, b = "hello", "world"
> t = { [a]=b }
> =t["hello"]
>
> is legal and prints "world"?

Yes, it's relevant, at least in that it proves me wrong :P and from
Tim's last post, it looks to be relevant to his question as well.

I completely forgot about the {[a]=b} notation (in other words, I only
remembered the syntactic sugar variants of table construction, and not
their true form... shame on me). Erm, conclusion: Keys in table
constructors do not need to be immediate values, when using that
notation.


2008/9/21 Tim Channon <tc@gpsl.net>:
> I've put something simple together which might give a clue on what I mean.
>
> require"tprint"
>
> var="fred"
>
> pa1="john"
> va1="sam"
> pa2="ken"
> va2="sal"
>
> rt={}
>
> rt[var]={[pa1]=va1}
>
> rt[var]={[pa2]=va2} -- does not add pair
>[...]

Firstly, for the record, the table constructor notation used here, is
the same variant as the one Geoff used above to smack some sense into
me ;)

The variable var, used as index in the last two statements, resolves
to "fred" in both those statements. So, since keys in Lua tables are
unique, in the second statement you're simply overwriting the entry
you created in the first statement (i.e. the entry named rt.fred). If
you change var to something else before the second statement (or in
any other way change what the expression used as index there resolves
to), then there should be no problem.