0

Am really struggling very much in deleting an item from table, am creating the table as dynamically,and inserting values in table working really great, but removing values from table making me really trouble. here is my script :

local key = isValueExists ( blowUpArray, spriteNo )
if key ~= nil then
table.remove ( blowUpArray, key )
end

function isValueExists(tbl, item)
    for key, value in pairs(tbl) do
        if value == item then 
            return key 
        end
    end
    return nil
end

what am doing wrong here ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ssss05
  • 717
  • 7
  • 14
  • 26

1 Answers1

0

Here is the simple one,

for k = 1, #blowUpArray do
    if tonumber(blowUpArray[k]) == spriteNo then
        table.remove (blowUpArray, k)
        end
    end
ssss05
  • 717
  • 7
  • 14
  • 26
  • So, `blowUpArray` had *strings*, not numbers! BTW, you can `break` once you have found the item, can't you? – lhf Oct 09 '12 at 11:13
  • The OP's most recent comment suggests the opposite actually. `#table` only counts integer based keys. The table consists of integers, but in the context above, k == tonumber(blowUpArray[k]); -- (ie both are numbers) – Hugh Dec 09 '12 at 09:40