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

Just some quick table help?

Started by Knovox, 01 April 2017 - 10:56 PM
Knovox #1
Posted 02 April 2017 - 12:56 AM
Hello!

I'm sure there is an easy way to do this… but I can't figure it out for the life of me.

What I am trying to do is create a config file. I have an API that automatically opens the table unserialized, so that I can use what is in the table for my settings in my program etc. The issue for me is how I can "edit" a certain item in the table.

My keys in the table are not numbers (like, table={ [ 1 ]="A value here" }), but they are like variables (table={ something="a value here" }). This works great, but changing a specific item is't going so well.

Okay, the problem is that I want to first remove the item from the table using table.remove(mytable, key), and insert the new "modified" item back into the table using table.insert(mytable, key, value) or something like that. However both table.insert and table.remove ask for numbers, not strings. Is there a way around this?

Also, when inserting an item into a table, is there a way to insert an item like a variable that contains a value (like how I have my table set up originally)?

Thanks! I will appreciate anyone's help. I can provide any extra information or pictures if needed. I just want this resolved at some point.
Bomb Bloke #2
Posted 02 April 2017 - 01:27 AM
Okay, the problem is that I want to first remove the item from the table using table.remove(mytable, key), and insert the new "modified" item back into the table using table.insert(mytable, key, value) or something like that. However both table.insert and table.remove ask for numbers, not strings. Is there a way around this?

A numerically indexed table has an order. Your string-based table does not. The purpose of insert / remove is to add / subtract indexes while maintaining order, hence they have no bearing on your table structure.

If you want to replace the data assigned to a given key, then simply perform a regular assignment:

local table = {["something"] = "a value here"}
table["something"] = "a new value here"

-- And also bear in mind that you can do stuff like this:
local keyName = "something"
print(table[keyName])

If you want to remove a key (and don't need to reshuffle your table's order), then just assign nil to it. Note that there's no point in doing this if you plan on immediately assigning some other value afterwards; just perform that assignment and be done with it. This is true whether you're using numeric indexes or other key types.
Knovox #3
Posted 03 April 2017 - 12:50 AM
Okay, the problem is that I want to first remove the item from the table using table.remove(mytable, key), and insert the new "modified" item back into the table using table.insert(mytable, key, value) or something like that. However both table.insert and table.remove ask for numbers, not strings. Is there a way around this?

A numerically indexed table has an order. Your string-based table does not. The purpose of insert / remove is to add / subtract indexes while maintaining order, hence they have no bearing on your table structure.

If you want to replace the data assigned to a given key, then simply perform a regular assignment:

local table = {["something"] = "a value here"}
table["something"] = "a new value here"

-- And also bear in mind that you can do stuff like this:
local keyName = "something"
print(table[keyName])

If you want to remove a key (and don't need to reshuffle your table's order), then just assign nil to it. Note that there's no point in doing this if you plan on immediately assigning some other value afterwards; just perform that assignment and be done with it. This is true whether you're using numeric indexes or other key types.

Thank you so much! That makes more sense… I knew it would be something so simple. Very helpful for sure!