lua -- next

来源:互联网 发布:sentinel 2数据 envi 编辑:程序博客网 时间:2024/06/05 10:51

next 允许程序遍历表中的每一个字段,返回下一索引和该索引的值。

local a = {a = "a", b = "b", c = "c", d = "d"}local valuewhile next(a, value) do    print(next(a, value))    value = next(a, value)end

输出如下:这里写图片描述
next函数原型是next(table[,index])
table:要遍历的表
index:next返回的值即是index索引的下一个值,当index为nil时,将返回第一个索引的值,当索引号为最后一个索引或表为空时将返回nil

注意:next(table)可以判定表table是否为空
所以判定表是否为空的方法有:
1、#table,当table为数组是直接返回table表长度
2、当table是字典时,返回table的长度

function table.size(t)    local s = 0;    for k,v in pairs(t) do        if v ~= nil then s = s+1 end    end    return s;end

3、next(table)

原创粉丝点击