Type – LUA table in the table shows zero

I am learning the code in Lua and encountered a problem with my code. This is the first time I use a table. Whenever I call a specific table in another table, My type is nil, and the print table does not show the table: xxxxx, because it usually does (it just prints a blank), I assume that means I did something wrong when defining the table?

Code:

local craft = {sword = {"cobble", stick = {}} }

print(type(craft.sword[1]))
print(craft.sword[1])
print(type(craft.sword[2]))
print(craft. sword[2])
print(craft)

(This is a simplified version of the table, used to test this error, but it will still appear here)

Output :

string
cobble
nil

table: 8a3b983

craft.sword contains key 1 and “stick” instead of 1 and 2. What you did is equivalent to:

local craft = {}
craft.sword = {}
craft.sword[1] = "cobble"
craft.sword.stick = {}< /pre>

Please note that nothing is stored in craft.sword[2] at any time.

More generally, if you use values ​​instead of keys to declare tables, such as {" one","two","three"}, then assign them to a numeric index, starting from 1. If you declare it with a key and value, as in {first = "one", second = "two", third = " three”}, these values ​​are only stored in the key you specify, and nothing is automatically stored in the digital index. If you mix them, as you do here-use {"cobble", stick = { }}-Values ​​without keys ("cobble") are automatically assigned to the numeric index, and those do(stick = {}) are assigned to the keys you specify.

Complete about how the table declaration works The specification is explained in the manual under Table Constructors.

I am learning the code in Lua and encountered a problem with my code. This is the first time I use With a table, whenever I call a specific table in another table, my type is nil, and the printed table does not show the table: xxxxx, because it usually will (it just prints a blank), I assume it means I What did you do wrong in defining the table?

Code:

local craft = {sword = {"cobble", stick = {}} }

print(type(craft.sword[1]))
print(craft.sword[1])
print(type(craft.sword[2]))
print(craft. sword[2])
print(craft)

(This is a simplified version of the table, used to test this error, but it will still appear here)

Output :

string
cobble
nil

table: 8a3b983

craft.sword contains keys 1 and "sticks" instead of 1 and 2. What you do is equivalent to:

local craft = { }
craft.sword = {}
craft.sword[1] = "cobble"
craft.sword.stick = {}

Please note that at any time Nothing is stored in craft.sword[2].

More generally, if you use values ​​instead of keys to declare tables, such as {"one","two","three"}, Then assign them to a numeric index, starting from 1. If you declare it with a key and value, such as in {first = "one", second = "two", third = "three"}, these values ​​are only stored where you specify And nothing is automatically stored in the digital index. If you mix them, as you do here-use {"cobble", stick = {}}-no key value ("cobble") Are automatically assigned to numeric indexes, and those do(stick = ()) are assigned to the keys you specify.

The complete specification of how table declarations work is explained in the manual under Table Constructors.

Leave a Comment

Your email address will not be published.