Code: Select all
for k,v in pairs(tbl) do --[[ code ]] end
Code: Select all
for k,v in pairs(tbl) do --[[ code ]] end
Looks like you're right; however, ipairs doesn't work if you happen to be using the table as a spare array or map:Nixola wrote:Actually, ipairs is a bit faster than pairs, and the latter also iterates over every string index of the table
Code: Select all
sparse = { [1] = 'one', [3] = 'three', [7] = 'seven' }
for k,v in pairs(sparse) do print(k,v) end
-- some order of ...
1 one
3 three
7 seven
for k,v in ipairs(sparse) do print(k,v) end
1 one
map = { foo = true, bar = false }
for k,v in pairs(map) do print(k,v) end
-- some order of ...
foo true
bar false
for k,v in ipairs(map) do print(k,v) end
-- nothing
Code: Select all
entities = {
player = Player(...),
}
for i = 1, NUM_OF_ENEMIES do table.insert(entities, Enemy()) end
for k,v in pairs(entities) do print(k,v) end
-- hits all the items in entities
for k,v in ipairs(entities) do print(k,v) end
-- skips player
Users browsing this forum: No registered users and 3 guests