The function ‘ipairs’ is a Lua method for iterating through a table with numerical indices. Consider the following table:
[lua]myTable = {}
table.insert(myTable, #myTable + 1, “some value”)
table.insert(myTable, #myTable + 1, “another value”)
– This table can be accessed by number
print(myTable[1]) --prints “some value”
print(myTable[2]) --prints “another value”[/lua]
That table has numbers for indexes. If you created another table with keys instead of indexes, it would be necessary to use the ‘pairs’ function instead.
[lua]myTable = {}
myTable[“width”] = 50
print(myTable[1]) --prints ‘nil’
print(myTable[“width”]) --prints 50
–Indexes cannot be strings, they would be keys instead
myTable[“1”] = “some value”
myTable[1] = “second value”
print(myTable[1] == myTable[“1”]) --prints ‘false’
–Compare the output of the following two statements to see the difference
for key, value in pairs(myTable) do print(‘used pairs here’, key, value) end
for i, value in ipairs(myTable) do print(‘used ipairs here’, i, value) end[/lua]
In your previous example, ipairs was used to iterate through the numeric indices of the table and the variable ‘line’ refers to the value in the table at each index i. The ‘line’ variable could be used instead of referencing by [i]. Be careful, the string you are providing as the path to an image is not a Corona display object so it cannot be inserted into a display group. Try something like this instead:
[lua]for i, line in ipairs(images) do
local myImage = display.newImage(line)
myImage.x, myImage.y = display.contentCenterX, display.contentCenterY
myLocalGroup:insert(myImage)
end[/lua] [import]uid: 168249 topic_id: 31049 reply_id: 124168[/import]