Just a simple thread where we can post Lua tricks, and some tips that the lua documentation is unclear on.
Trick #1: Swapping variables
local a, b = b, a
Tip #1: Difference between ipairs and pairs
output:
Trick #1: Swapping variables
local a, b = b, a
Tip #1: Difference between ipairs and pairs
Lua:
local foodTableIPAIRS = {"banana", "banana", "vegetable"}
local foodTablePAIRS = {["apple"] = "fruit", ["banana"]="fruit", ["lettuce"]="vegetable"}
print("Pairs:\n")
for k, v in pairs(foodTablePAIRS) do
print(k .. " = " .. v .. "\n")
end
print("\nIpairs:\n")
for k, v in ipairs(foodTableIPAIRS) do
print(k .. " = " .. v .. "\n")
end
output:
Pairs:
apple = fruit
banana = fruit
lettuce = vegetable
Ipairs:
1 = fruit
2 = fruit
3 = vegetable
Last edited: