বিষয়বস্তুতে চলুন

মডিউল:table/reverseIpairs

উইকিঅভিধান, মুক্ত অভিধান থেকে

এই মডিউলের জন্য মডিউল:table/reverseIpairs/নথি-এ নথিপত্র তৈরি করা হয়ে থাকতে পারে

local ipairs = ipairs

--[==[
Iterates through a table using `ipairs` in reverse.

`__ipairs` metamethods will be used, including those which return arbitrary (i.e. non-array) keys, but note that this function assumes that the first return value is a key which can be used to retrieve a value from the input table via a table lookup. As such, `__ipairs` metamethods for which this assumption is not true will not work correctly.

If the value `nil` is encountered early (e.g. because the table has been modified), the loop will terminate early.]==]
return function(t)
	-- `__ipairs` metamethods can return arbitrary keys, so compile a list.
	local keys, i = {}, 0
	for k in ipairs(t) do
		i = i + 1
		keys[i] = k
	end
	return function()
		if i == 0 then
			return nil
		end
		local k = keys[i]
		-- Retrieve `v` from the table. These aren't stored during the initial
		-- ipairs loop, so that they can be modified during the loop.
		local v = t[k]
		-- Return if not an early nil.
		if v ~= nil then
			i = i - 1
			return k, v
		end
	end
end