Minor problem with tables (I think)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Minor problem with tables (I think)

Paul Bleisch
lua-l

I was messing around trying to use lua as a model definition
system and came across a small problem I think.  For some reason:

Foo = {
  Name="Foo",
  {0,1,2,3,4}, -- <-- here see note below
  {1,2,3,4},
  {2,3,4},
  {3,4}
};

The problem is that lua cannot seem to handle unnamed and named
fields in a table constructor.  i.e. The above example fails with
a syntax error on line 3 (marked above) and the last token is '{'.
If I remove the Name="Foo", line everything is hoopy.  Looking
at the definition in the documentation, I can see why this is an
error, but I don't understand why it can't be handled.  

If I understand things correctly, I should do:
Foo = {
  [Name]="Foo",
  {0,1,2,3,4}, 
  {1,2,3,4},
  {2,3,4},
  {3,4}
};

But this doesn't seem to work either.  My goal is to allow
people to describe relatively complex heirarchies of objects
with the pretty simple syntax of table constructors.  I don't
want to have to do something like:
Foo = {
  Name="Foo",
  Foo0={0,1,2,3,4}, 
  Foo1={1,2,3,4},
  Foo2={2,3,4},
  Foo3={3,4}
};


Any ideas?

Paul
--
Paul Bleisch
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Minor problem with tables (I think)

Stephan Herrmann-2
Paul Bleisch <[hidden email]> writes:

> lua-l
> 
> I was messing around trying to use lua as a model definition
> system and came across a small problem I think.  For some reason:
> 
> Foo = {
>   Name="Foo",
>   {0,1,2,3,4}, -- <-- here see note below
>   {1,2,3,4},
>   {2,3,4},
>   {3,4}
> };
Is it acceptable for you, to write:

 Foo = {
   {0,1,2,3,4}, 
   {1,2,3,4},
   {2,3,4},
   {3,4};
   Name="Foo"
 };

which is equivalent to 

Foo[1]={0,1,2,3,4}
Foo[2]={1,2,3,4}
Foo[3]={2,3,4}
Foo[4]={3,4}
Foo[Name]="Foo"

Note the semicolon between the list-part and the hashtable-part. The order
of these two parts is significant.

Greetings
Stephan

--------------------------------------------------------------------------
 Stephan Herrmann
 ----------------
 Technical University Berlin
 Software Engineering Research Group
 http://swt.cs.tu-berlin.de/~stephan
 phone: ++49 30 314 73174         
--------------------------------------------------------------------------