lua学习笔记15:table数组逆序

数组的逆序,只能用于数组,不能用于哈希表


function reverseTable(tab)
	local tmp = {}
	for i = 1, #tab do
		local key = #tab
		tmp[i] = table.remove(tab)
	end

	return tmp
end

// 示例
local t = {"one", "two", "three"}

for k, v in pairs(t) do
	print(k, v)
end

t = reverseTable(t)

for k, v in pairs(t) do
	print(k, v)
end


输出:

1 one
2 two
3 three
1 three
2 two
3 one




你可能感兴趣的:(lua)