迭代器for遍历table时,ipairs和pairs的区别: 区别一:ipairs遇到nil会停止,pairs会输出nil值然后继续下去 区别二: , b = , x = , y = , "Good", nil, "Bye"} -- for i,v in ipairs(a) do -- print(v) -- end for k,v in pairs(a) do print(k,v) end 可见:ipairs并不会输出table中存储的键值对,会跳过键值对,然后…
为了看出两者的区别,首先定义一个table: a={"Hello","World";a=1,b=2,z=3,x=10,y=20;"Good","Bye"} 使用ipairs对其进行遍历: for i, v in ipairs(a) do print(v) end 输出的结果是: HelloWorldGoodBye 可见ipairs并不会输出table中存储的键值对,会跳过键值对,然后按顺序输出table中的值. 再使用pair…
官方描述: ipairs (t) Returns three values: an iterator function, the table t, and 0, so that the construction for i,v in ipairs(t) do body end will iterate over the pairs (1,t[1]), (2,t[2]), ···, up to the first integer key absent from the table. pairs (…
在使用lua设计类时'.'和':'的区别主要在于使用'.'必须手动加self参数,使用':',可以隐藏这个参数,使用'.'调用使用':'定义的函数时,要注意,函数的第一个参数为self,如 function Class:new(x,y) end使用'.'调用时需要.new(self,x,y),若忘记写self,则x,y迁移,导致y参数nil…
pairs Returns three values: the next function, the table t, and nil, so that the construction for k,v in pairs(t) do body end will iterate over all key–value pairs of table t. See function next for the caveats of modifying the table during its traver…
t = { 1, 2, 3, nil, 4,} for k, v in ipairs(t) doprint(k, v)end print("---------------------------") for k, v in pairs(t) doprint(k, v)end t = {} , do table.insert(t, i*i) end t[] = nil t[] = nil print(#t) --10, not right --right way --~ 1 1 --~…