This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Drifter's profile picture

Delete table inside table

Started by Drifter, 20 April 2013 - 05:32 AM
Drifter #1
Posted 20 April 2013 - 07:32 AM
Hey, I have something like this:


resources = {["planks"]={["side"]="back",["code"]=4,},["cobble"]={["side"]="back",["code"]=2,},["dirt"]={["side"]="back",["code"]=1,},}
(3 tables inside one table)

Everything is working just fine. My question is: How to delete one specific table. For example I want delete cobble from resources. I tried use table.remove() but it didn't work.

Thnx for answer.
Kingdaro #2
Posted 20 April 2013 - 07:45 AM
table.remove() only works with numbers. Just set the index to nil.


resources.cobble = nil
Drifter #3
Posted 20 April 2013 - 08:00 AM
thnx, just made small change

name = read()
resources[name] = nil
remiX #4
Posted 20 April 2013 - 08:26 AM
You could have a table like this.
resources = {
    {block="planks",side="back",code=4},
    {block="cobble",side="back",code=2},
    {block="dirt",side="back",code=1},
}

But I guess then you would have to loop through the table checking the block string if it's the one you want.
Also won't have a table with a lot of nil indexes.
Popeye #5
Posted 20 April 2013 - 08:32 AM
What will the
side="back"
do?

Put the Item into the Crafting Table?
Kingdaro #6
Posted 20 April 2013 - 08:34 AM
@remiX
Setting a variable or table index to nil removes it; you would get nil from an undefined variable as you would from an unset variable. Setting something to nil is like not having set it at all.

"Having a bunch of nil indexes" is no problem, because there are already a lot of them in the table.
remiX #7
Posted 20 April 2013 - 08:41 AM
@remiX
Setting a variable or table index to nil removes it
Really? Oh. Cool xD

"Having a bunch of nil indexes" is no problem, because there are already a lot of them in the table.
Yeah true dat.

But will textutils.serialize not have the ones that have been set to nil?
Drifter #8
Posted 20 April 2013 - 08:43 AM
What will the
side="back"
do?

Put the Item into the Crafting Table?
No, it's a little complex. I have a row of 16 barrels, and every barrel has its own colored wire, together these wires are connected to one bundled cable. Thats why there is code for.
Side determines where is bundled cable connected to computer. So you can connect 6 bundled cables with 16 different wires (96 total).
Kingdaro #9
Posted 20 April 2013 - 08:45 AM
@remiX
Setting a variable or table index to nil removes it
Really? Oh. Cool xD

"Having a bunch of nil indexes" is no problem, because there are already a lot of them in the table.
Yeah true dat.

But will textutils.serialize not have the ones that have been set to nil?
If anything, serialize uses pairs(), which only iterates through non-nil values and their indices.

So yes, serialize will not show nil indices.
superaxander #10
Posted 20 April 2013 - 08:49 AM
So yes, serialize will not show nil indices.
Ah that's a shame that's why my program doesn't work :(/>
Kingdaro #11
Posted 20 April 2013 - 10:05 AM
You could try using "false" instead of nil.