next (table [, index])
!subtitle:说明
获取表 table 中 index 键的后一个键值对。
!subtitle:参数
table - 要获取键值对的表
index - 获取该键的下一个键值对;默认为 nil 表示获取第一个键值对
!subtitle:返回值
返回键值对
空表或 index 是最后一个键时返回 nil
-- 定义表
local fruits = {
apple = "红色",
banana = "黄色",
orange = "橙色",
grape = "紫色"
}
-- 获取第一个键值对
local key, value = next(fruits)
-- 循环遍历
repeat
print(key, value)
key, value = next(fruits, key)
until key == nil
next - Lua 5.4 Reference Manual