1. #1

    can you insert into a table by name or just by number

    I have a table

    fruit = {

    ["banana"] = "yellow"
    ["apple"] = "red"
    ["pear"] = "green"


    I want to insert

    ["grape"] = "purple"

    Can I do that? tinsert says it either just takes the value or a number and a value. I don't want either option because I'll just get

    [4] = "purple"

    So how do I do it?

  2. #2
    fruit.grape = "purple"

    Your example uses it as an associative/hash table, those behave different from indexed/sequential tables

    e.g. here is how you use table.insert()
    local colors = {
    tinsert(colors, "purple")
    print(colors[4]) -- "purple"
    Last edited by Ketho; 2020-03-30 at 05:05 AM.

  3. #3
    Or if you have the key in a variable
    local key = "grape"
    local value = "purple"
    fruit[key] = value

  4. #4

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts