判斷表為空的方法
目前為止,lua語言中判斷table表是否為空有三種方式:
(1)#table,當table為陣列時直接返回table表的長度。
(2)當table是字典時,返回table的長度
(3)next(table),利用next函式進行判斷。function table.size(t)
local s = 0;
for k, v in pairs(t) do
if v ~= nil then s = s + 1 end
endreturn s;
end
local t =
local k, v
while true do
k, v = next(t, k)
print(k ,v)
if not v then break end
end--[[ 執行結果
hello 1
lucy 3
world 2
nil nil
]]local tt = {}
function isempty(tbl)
if next(tbl) ~= nil then
print("is not empty.")
else
print("is empty.")
endendprint(isempty(t))
print(isempty(tt))
--[[ 執行結果
is not empty.
is empty.
]]
Lua 判斷表是否為空方法
判斷表為空的方法 目前為止,lua語言中判斷table表是否為空有三種方式 1 table,當table為陣列時直接返回table表的長度。2 當table是字典時,返回table的長度 1 function table.size t 2local s 0 3 for k,v in pairs t ...
lua判斷乙個表是否為空表
1 有人會直接用表與 比較 local a if a then print a 是空表 else print a 不是空表 end2 還有人可能用table.maxn local a if table.maxn a 0 then print a 是空表 else print a 不是空表 end a...
JQuery判斷是否為空
有如下三種判斷 var a val if a null a undefined a 這裡解釋一下null與undefined和 的區別 null 空物件 不是物件,轉為數值為0 undefined 全域性物件window的乙個特殊屬性.undefined表示 缺少值 就是此處應該有乙個值,但是還沒有...