Tables

I have been searching around the web for a way to check if for exaple the fish has the amount 0 then it should not be showed in DPanelList.

And also how to add a specific amount of let’s say bananas without increassing the amount of all the items.


InFridge = {}
InFridge[1] = {FridgeName = "Fish", FridgeModel = "models/props/CS_militia/fishriver01.mdl", Amount = 1}
InFridge[2]	= {FridgeName = "Blue Soda", FridgeModel = "models/props_junk/PopCan01a.mdl", Amount = 1}
InFridge[3]	= {FridgeName = "Milk", FridgeModel = "models/props_junk/garbage_milkcarton002a.mdl", Amount = 1}
InFridge[4]	= {FridgeName = "Water Melon", FridgeModel = "models/props_junk/watermelon01.mdl", Amount = 1}
InFridge[5]	= {FridgeName = "Water Bottle", FridgeModel = "models/props/cs_office/Water_bottle.mdl", Amount = 1}
InFridge[6]	= {FridgeName = "Beer", FridgeModel = "models/props_junk/garbage_glassbottle003a.mdl", Amount = 1}
InFridge[7]	= {FridgeName = "Chinese Takeaway", FridgeModel = "models/props_junk/garbage_takeoutcarton001a.mdl", Amount = 1}
InFridge[8]	= {FridgeName = "Banana", FridgeModel = "models/weapons/w_banana.mdl", Amount = 1}

[lua]
for i = 1, #InFridge do
if InFridge*.Amount == 0 then
return
end
end[/lua]

I’m half asleep so bare with me, but I’m fairly sure you can check if it’s 0 this way

Thanks alot.

but just one more question.

what does the “#” do?

Is it count how many tabels?

table.Count counts any table ( if keys are string, numbers, etc ), while #table only gives the number of elements which have a numerical key ( I don’t recall if they needed to be sequential or not ).

they do not need to be sequential

using # is better because your stuff doesnt doesnt have strings as indexes

[editline]25th June 2014[/editline]

[lua]for i = 1, #InFridge do
if InFridge*.FridgeName == “Banana” then
InFridge*.Amount = Amount + 1
end
end[/lua]

This is an entity so do the entire table reset when i press E on it?

becuse it seems like it.

You’ll get weird results if they aren’t sequential

Not sure what the goal is, but I usually prefer in pairs for use with my tables



for _,v in pairs(InFridge) do
  if (v.FridgeName == "Banana") then
    v.Amount = v.Amount + 100000
  end
end


Or if you need to programmatically access the Sub Table keys



for _,v in pairs(InFridge) do
  if (v["FridgeName"] == "Banana") then
    v["Amount"] = v["Amount"] + 100000
  end
end


Okey, I have created an fridge entity that when pressed on opens a menu with items (icons) and when clicking on them it spwns that selected item and sets the table (shown on the top) amount of that item to one less, which makes it to 0.

But when I press E on the fridge the very item that i just spawned is showed again but still has the amount 0.

Any tip how to fix this would be appreciated.

Here is the full code

Thank you.