Arrays help: table.indexOf not working?

I’m working on a project and I’d like to add a few objects to a table/array. I created my items, table, etc

local box = display.newImage("box.png")  
local board = display.newImage("board.png")  
  
local array = {}  
  
-- insert items into array, this is typically done programatically   
-- so they are not already added at creation of table  
  
table.insert(array, box)  
table.insert(array, board)  
  
local index = table.indexOf(array, box) -- this should return 1 right?  
print("Index: " .. index)  

Instead I get: attempt to call field ‘indexOf’ (a nil value)

So I thought it was returning a nil value so that was the issue. so I tried:

local index = table.indexOf(array, box)  
if (index == nil) then  
 print("It's nil")  
end  

still same result (Runtime Error)

I’m really wanting to remove items from my array. I tried searching through the array and removing the item. This works, but the array wont re-order the index value. So it thinks index 1 is still there but instead I later learned it leaves a “hole”. So I’d like to do this:

table.remove(array, table.indexOf(array, box))  

EDIT: Also, shouldn’t this work just fine as I see in the docs:

local tab = {1, 2, 3, 4, 5}  
print("Index is: " .. table.indexOf(tab, 4))  

Can anyone shed some light on this one?

Thanks for any and all help,

-d [import]uid: 27681 topic_id: 10991 reply_id: 310991[/import]

I’m pretty sure that table.indexOf has been removed. I looked under the Array programming guidelines and it was not mentioned. Only in the API is it listed. Instead I wrote this function:

------------------------------------  
-- remmoveFromArray() -- removes objects from our materails table  
local removeFromArray = function(object)  
 -- remove from array  
 --print("Array has: " .. #inGameMaterials)  
 local removeIndex = nil  
 for i=1,#inGameMaterials do  
 if (inGameMaterials[i] == object) then  
 --print("Found it")  
 removeIndex = i  
 end  
 end   
 if (removeIndex == nil) then  
 --print("Not Found")  
 return  
 else  
 --print("Removing from array")  
 table.remove(inGameMaterials, removeIndex)  
 removeIndex = nil  
 --print("Removed.")  
 end  
  
 print("Now Array has: " .. #inGameMaterials)  
 for i = 1, #inGameMaterials do  
 print("Name: " .. inGameMaterials[i].name .. " At index: " .. i)  
 end  
end  
  

The print() statements are there for debugging so you can see it actually working. Just pass in the object, it compares it against objects in the table, saves the index, and then deletes it in the end.

Hope that helps. [import]uid: 27681 topic_id: 10991 reply_id: 40139[/import]