Jump to content




Just some quick table help?

lua help

2 replies to this topic

#1 Knovox

  • Members
  • 7 posts
  • LocationCalgary, AB, Canada

Posted 01 April 2017 - 11:56 PM

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.

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 02 April 2017 - 12:27 AM

View PostKnovox, on 01 April 2017 - 11:56 PM, said:

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.

#3 Knovox

  • Members
  • 7 posts
  • LocationCalgary, AB, Canada

Posted 02 April 2017 - 11:50 PM

View PostBomb Bloke, on 02 April 2017 - 12:27 AM, said:

View PostKnovox, on 01 April 2017 - 11:56 PM, said:

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!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users