table.insert - Overrides my previous keys?

This is what I currently have:
[lua]
local inventory = {}

local temp_guns = {
	["weapon_physgun"] = "models/weapons/v_superphyscannon.mdl",
	["weapon_357"] = "models/weapons/v_superphyscannons.mdl"
}
for k, v in pairs(temp_guns) do
	print(k,v)
	if (self:GetActiveWeapon():GetClass() == k) then
		local class = self:GetActiveWeapon():GetClass()
		self:StripWeapon(class)
		table.insert(inventory, table.getn(inventory) + 1, class)
	end
end
self.inventory = inventory

[/lua]

Main issue:
I use the console command that activates this function. When it inserts the table the index of 1 shows up with the weapon class as the value. When a second weapon is stripped the first index is overridden by the second weapon cause it to look like this.

Strip first weapon:


1:
		weapon_physgun	=	true

Strip second weapon:


1:
		weapon_357	=	true

Could someone please explain what is wrong with this?

table.getn is actually depreciated as of Lua 5.1. A proper way of getting the number of elements is by using the pound symbol in-front of your table name, like so:

[lua]
for k,v in pairs( someTable ) do
table.insert(anotherTable, #someTable + 1, v);
end
[/lua]

Also, you don’t have to set the local ‘class’ inside of the if statement, instead use the ‘k’ in the for loop.
This works because you are saying, if the players active weapon is equal to this particular element in the table, then do blah blah blah.
So because k represents our current weapons class, we can use it as the class instead of defining another variable.

[lua]
for k, v in pairs(temp_guns) do print(k,v)
if (self:GetActiveWeapon():GetClass() == k) then
self:StripWeapon(k)
table.insert(inventory, #inventory + 1, k)
end
end
[/lua]

Hope this helps with your problem.

Thanks but I already tried:
table.insert(anotherTable, #someTable + 1, v)
and it didn’t work either all it did was override the last key which was the first displayed in my OP.

That’s why I tried the table.getn and so on…

Updated:
Tried your way:
[lua]
for k, v in pairs(temp_guns) do
print(k,v)
if (self:GetActiveWeapon():GetClass() == k) then
self:StripWeapon(k)
table.insert(inventory, #inventory + 1, k)
end
end
[/lua]
Console:


1	=	weapon_physgun
1	=	weapon_357

Table.Insert() is still not working properly.

I even tried:
[lua]
for k, v in pairs(temp_guns) do
print(k,v)
if (self:GetActiveWeapon():GetClass() == k) then
self:StripWeapon(k)
inventory[#inventory + 1] = k
end
end
[/lua]

Still doesn’t work.

Update:
Apparently table is nil?
Even after changing the name it’s nil and it’s still overwritten:
[lua]
for k, v in pairs(temp_guns) do
print(k,v)
if (self:GetActiveWeapon():GetClass() == k) then
table.insert(temp_inv, k)
self:StripWeapon(k)
print(temp_inv[#temp_inv + 1])
end
end
[/lua]

Declare inventory outside the function.

-snip new thread-
table.insert() solved thank you ralle.

Lol, I had not even thought about the scope. Anywho, table.getn is still depreciated.

why did no one tell him that he’s doing linear search on a hash table?

brandon: just use this, it’s way faster:
[lua]local temp_guns = {
[“weapon_physgun”] = “models/weapons/v_superphyscannon.mdl”,
[“weapon_357”] = “models/weapons/v_superphyscannons.mdl”
}

function yourfunction(yourargs)
local class = self:GetActiveWeapon():GetClass()
local model = temp_guns[class]
if (model) then
self:StripWeapon(class)
self.inventory = { class }
else
self.inventory = {}
end
end
[/lua]

I’ve been helping him via PM’s for the past few days…

And that is certainly something I have never thought of doing… Constant performance. I like it…