Advertisement

Lua: trouble accessing indexed tables in construct

Started by June 17, 2004 12:18 AM
1 comment, last by RolandofGilead 20 years, 5 months ago
...ors" It took many hours to figure this out because it seems like such a stumbling block for such a powerful language but basically I cannot do this for some strange reason.

grid = {false, true}
environment[1] = {
  grid[1] = { 1; 10; 50; 50; };
  framerate = 24;
};
but I can do this

grid = {false, true}
grid[1] = "post";
environment[1] = {
  grid = { 1; 10; 50; 50; };
  framerate = 24;
};
this also works and suggests a solution

grid = { {267, 2983, 2901, 677}, 278, 298 };
environment[1] = {
  grid = { 1; 10; 50; 50; };
  framerate = 24;
};
environment[1].grid = grid[1];
This is all so messy cause I'm generating the code and I'd like nearly every variable to be part of a unified list of all others like it but without duplication. Changing the listed variable([x]) will change the structued variable(foo.bar.what) if they are both tables but that leaves out things like the framerates. So I guess I could just create empty tables to start with and create a buffer to create the assignments after the constructors. The lua-users.org mailing list is being updated so I can't search it.
Quote: Original post by RolandofGilead

...I cannot do this for some strange reason.


There are two things wrong with your first block of code...and if you've struggled with it for hours I'll try to get you started but I really suggest you go over the table constructor part of the manual carefully...

First, the idiom var = { ... } is a table constructor. It's what you use, simply, to create a table. But you're table constructor reads environment[1] = { ... }. So you're trying to ACCESS the value in table "environment" with key 1, the problem being that an "enviroment" table hasn't been created yet! Error.

There are two "methods" in which tables are usually constructed in Lua, and both have their advantages and disadvantages. The first is the one you're doing...a constructor "block" enclosed by braces...inside the braces is a sequence of "key = value" pairs. If you leave out the "key =" part, then the values are given numerical indexes starting at 1. For example...

-- hey its a table constructor! yaymytable = {  foo = "some text",  bar = function() for i=1,10 do print "hello!" end end,  aNumber = 44,  "stuff",  "more stuff",  "yet more stuff",}-- here are some examples for the above table, and outputsprint(mytable.foo)         -- some textprint(mytable.aNumber - 3) -- 41print(mytable[1])          -- stuff   <-- because "stuff" is the first entry without a key!print(mytable[3])          -- yet more stuffmytable.bar()              -- (hello!) x 10


Another common way to construct tables can be more readable. You "declare" it first, and then you can assign keys and values later:

entry = {}entry.name = "John Doe"entry.age = 35


Advertisement
Thanks for trying to help, but...

Quote: Original post by Anonymous Poster

Quote: Original post by RolandofGilead

...I cannot do this for some strange reason.


There are two things wrong with your first block of code

Could you elaborate on the second?

Quote: First, the idiom var = { ... } is a table constructor. It's what you use, simply, to create a table. But you're table constructor reads environment[1] = { ... }. So you're trying to ACCESS the value in table "environment" with key 1, the problem being that an "enviroment" table hasn't been created yet! Error.


Sorry, I should have said I was getting syntax errors and not run-time errors. I thought it was covered because in all the examples, environment is accessed the same and in some of them it runs, as I said, implying that environment[1] wasn't causing the problem.

I also could have explained it better if the title were allowed to be longer. I'm parsing a file and generating the code. I want to make each instance of a variable, no matter which one, a member in a numerically indexed master list of all the other variables like it, but I also want each table to have a member with the appropriate name. You see, I was trying to make a member of a table be also the member of another table, which you cannot apparently do. I can assign the member of a table to the value found somewhere else, but I cannot make accessing one, fully equal to accessing the other.

Oh, my goodness, I just thought of a way to do it, but it's not that great. I could generate the code to assign the next in the array the appropriate value, and in the other, when it's accessed as a named member of a table, I simply make the named member equal to the index it takes up in the master list. That would require retrieving the name of the variable as a string, which I'm not sure how to do, but if nothing else, instead of equalling an index I can make it a table with the index and a string which is its name.

This topic is closed to new replies.

Advertisement