如何增加表中的变量并访问它?

时间:2014-10-13 13:54:52

标签: lua

我有一个表定义如下:

local counter = {    
deleted = 0,    
moved = 0,    
sumOfFileSize = 0,    
}    

我尝试过以下方法来增加或获取值,但它不起作用:

增量:counter.deleted = counter.deleted + 1

获取:counter.deleted

如何成功修改或阅读这些值?

1 个答案:

答案 0 :(得分:0)

如果你在某些功能中使用你需要声明whitout本地,而你可以用文件顶部声明它

counter={deleted = 0,
 moved = 0,
 sumOfFileSize = 0}

用于访问您想要的所有功能。

问题来自另一个地方,因为我在控制台中尝试这个,这里是结果,所以问题不是lua

counter = {
 deleted = 0,
 moved = 0,
 sumOfFileSize = 0,
 }
 print(counter)
table: 0x7fb1e2e005d0
 counter.deleted = counter.deleted + 1

 print(counter.deleted)
1
 counter.deleted = counter.deleted + 1
 print(counter.deleted)
2
 counter.deleted = counter.deleted + 1
 print(counter.deleted)
3