lua中的table

mytable = { hello}
print(table.getn(mytable))
--结果为0 不加引号不计入元素的 不加引号表示索引值, 如果赋值可以用mytable["hello"]或mytable.hello来访问

mytable = { "hello" }
print(table.getn(mytable))
--结果为1, 表示mytable[1元素的值为"hello"字符串


mytable =
{
    hello = "hello",world = "world",
    {
        x = 100, y = 100
    };
    {
        x = 200, y = 200
    }
}

print(table.getn(mytable))

for i = 1, table.getn(mytable)
do
    str = string.format("index %d: x = %d, y = %d", i, mytable[i].x, mytable[i].y)
    print(str)
end

print(mytable["hello"])
print(mytable.world)

mytable = { hello, world}
print(table.getn(mytable))
print(mytable.hello, mytable[1])
--0
--
nil nil


mytable = { "hello"}
print(table.getn(mytable))
print(mytable.hello, mytable[1])
--1
--
nil nil
--
----------------结果如下-------------------

>lua -e "io.stdout:setvbuf 'no'" "test.lua" 
2
index 1: x = 100, y = 100
index 2: x = 200, y = 200
hello
world
0
nil    nil
1
nil    hello
>Exit code: 0

--lua表中可以用,也可以用分号, 没区别 

原文地址:https://www.cnblogs.com/barrysgy/p/2345023.html