table.remove in for k,v in pairs

So I have some code, that loops through an inventory, like so.
[lua]
for k,v in pairs(inventory) do
if k == itemClass and v == 1 then
table.remove(inventory, inventory[k])
itemRemoved = true
self:SetNWString(“rp_inventory”, glon.encode(inventory))
self:SaveData()
return
elseif
k == itemClass and v > 1 then
if v - quantity >= 0 then
inventory[k] = v - quantity
itemRemoved = true
self:SetNWString(“rp_inventory”, glon.encode(inventory))
self:SaveData()
return
else
self:Notify(“You cannot drop that many”)
end
end
index = index + 1
end
if !itemRemoved then
self:Notify(“You do not have that item”)
end
[/lua]

due to a recent change of format to my inventory, I moved from
[lua]
inventory = { [1] = { item = “milk”, quantity = 3, }} [[TO]] inventory = { “milk” = 3,}
[/lua]
Now the first snippet of code is used to loop over the inventory and remove the quantity of an item with my old structure I could simply use
[lua]
table.remove(inventory, i)
[/lua]
because I used;
[lua]
for i=1,#inventory do
[/lua]
but now I am forced to use for k,v because I need to use the key to identify my item.

I’ve had a shot at
[lua]
table.remove(inventory, inventory[k])
[/lua]
But to no avail.

There is no question in your post.

I guess I got ahead of myself,

I need to remove the entry from the table if the quantity is 1.

in this code;
[lua]
if k == itemClass and v == 1 then
table.remove(inventory, inventory[k])
itemRemoved = true
self:SetNWString(“rp_inventory”, glon.encode(inventory))
self:SaveData()
return
[/lua]

[editline]30th June 2012[/editline]

table.remove(inventory, inventory[k]) doesn’t work, it’s just in there

inventory[k] = nil should work.

I’m so thick. Cheers.

Or table.remove(inventory, k).

What’s the difference between tbale.remove and setting the table to nil anyway?
Does table.remove do something that the other method doesn’t like removing the table from the memory?

setting the table to nil is pretty much the internal functionality of table.remove…

Wrong, table.remove only works for numeric index and only if the table is exclusively numeric.

table.remove furthermore pops a value, which means all the other values move down 1 index.

setting an index value to nil however just removes the reference stored at that special key.