[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Nested table constructor with reference to other children
- From: "Thijs Schreijer" <thijs@...>
- Date: Sun, 30 Oct 2011 16:03:38 +0100
> I am trying create a table with subtables, where subtables reference
> each other, using the table constructor syntax:
>
> a = {
> one = {
> a = 1,
> b = 2
> },
> two = {
> a = 1,
> b = one -- set's b to nil
> }
> }
>
> Is there a way to write this constructor, so that a.two.b is set to
> a.one instead of nil? Or do I have to fixup the table after
> construction?
>
The line 'b = one' should at least be 'b = a.one' to refer properly. But it
then still won't work because (iirc) the righthand side of 'a = { ...' is
evaluated before it is being assigned to 'a', so 'b = a.one' will deliver a
'attempt to index global 'a'' error.
Try this;
a = {}
a.one = {
a = 1,
b = 2
}
a.two = {
a = 1,
b = a.one
}
Regards,
Thijs