'for k,v in pairs(mytable) do'
expand the variable names and it comes closer to natural language:
'for key, value in pairs(mytable) do
-- stuff with key
-- or stuff with value
end
local vehicles = {"bicycle", "car", "truck"}
this table has implicit integer keys it is the equivalent of
local vehicles = {
[1] = "bicycle",
[2] = "car",
[3] = "truck",
}
ipairs works on a subset of lua tables that has sequentially indexed values like above.
for index, value in ipairs(vehicles) do
print("vehicle:"..value)
end
The table above is one where the information (the vehicle) is held by the value.
ipairs will not work on a table with non-sequential integer keys or other type of keys.
for example:
local polluters = {
["bicycle"] = false,
["car"] = true,
["truck"] = true,
}
pairs has to be used here
for key, value in pairs(polluters) do
if polluters[key] then
print("gas masks on")
end
end
|