'LUA - How can you generate an array of numbered variables?

I'm sorry if my question is stupid, I'm not a programmer, but I'd really like to write a tool to help me generate color gradients.

Until now I've had this huge table where I stored calculation data for each variable.

    local Hhu1 = hueCalc("hhu",     0,  0,          0,          0,          0,          0       )
    local Hhu2 = hueCalc("hhu",     1,  360/18  ,   360/12,     360/8,      360/6,      360/4   )
    local Hhu3 = hueCalc("hhu",     2,  360/18*2,   360/12*2,   360/8*2,    360/6*2,    360/4*2 )
    local Hhu4 = hueCalc("hhu",     3,  360/18*3,   360/12*3,   360/8*3,    360/6*3,    360/4*3 )
    local Hhu5 = hueCalc("hhu",     4,  360/18*4,   360/12*4,   360/8*4,    360/6*4,    0       )
    local Hhu6 = hueCalc("hhu",     5,  360/18*5,   360/12*5,   360/8*5,    360/6*5,    0       )
    local Hhu7 = hueCalc("hhu",     6,  360/18*6,   360/12*6,   360/8*6,    0,          0       )
    local Hhu8 = hueCalc("hhu",     7,  360/18*7,   360/12*7,   360/8*7,    0,          0       )
    local Hhu9 = hueCalc("hhu",     8,  360/18*8,   360/12*8,   0,          0,          0       )
    local Hhu10 = hueCalc("hhu",    9,  360/18*9,   360/12*9,   0,          0,          0       )
    local Hhu11 = hueCalc("hhu",    10, 360/18*10,  360/12*10,  0,          0,          0       )
    local Hhu12 = hueCalc("hhu",    11, 360/18*11,  360/12*11,  0,          0,          0       )
    local Hhu13 = hueCalc("hhu",    12, 360/18*12,  0,          0,          0,          0       )
    local Hhu14 = hueCalc("hhu",    13, 360/18*13,  0,          0,          0,          0       )
    local Hhu15 = hueCalc("hhu",    14, 360/18*15,  0,          0,          0,          0       )
    local Hhu16 = hueCalc("hhu",    15, 360/18*16,  0,          0,          0,          0       )
    local Hhu17 = hueCalc("hhu",    16, 360/18*17,  0,          0,          0,          0       )

that was used to display different shades to this piece of UI

:shades
    {
        id = "paletteHardHue",
        label = "Hard Hue",
        colors = {Hhu1, Hhu2, Hhu3, Hhu4, Hhu5, Hhu6, Hhu7, Hhu8, Hhu9, Hhu10, Hhu11, Hhu12, Hhu13, Hhu14, Hhu15, Hhu16, Hhu17, Hhu18},
        onclick = function(ev)
            if (ev.button == MouseButton.LEFT) then
                app.fgColor = ev.color
            end
        end
    }

But this is neither scalable, nor do I need to store these informations, because I could imply all these calculations by the maximum amount of colors I want to generate and their position.

Also, with my approach, I'm generating a lot of unnecessary, empty colors.

If I could generate this kind of array, it would erase a whole set of problems for me:

{Hhu1, Hhu2, Hhu3, Hhu4, Hhu5, Hhu6, Hhu7, Hhu8, Hhu9, Hhu10, Hhu11, Hhu12}

But I only know how to generate an array purely made of numbers, and this here obviously doesn't work:

function()
        array = {}
        for i = 1, 10 do
            array["VARNAME"i] = 0
        end
        }

Thank you very much for any help in advance!!!



Solution 1:[1]

I believe you have already known the proper way to generate your color array, You don't need to define identifiers to make such array.

But if you really want to, you can make use of _G and _ENV, these are two special tables stores program environment. In Lua there's no global variables, just this two table.

Every access to name that has no definition in current scope (or you can say a upvalue) will be redirected to field with that name in _ENV table. And as every normal table access, if not such field exists, you got a nil value, but not error.

With _ENV, you can define variable with dynamic name:

print(_ENV.a)

a = true
print(_ENV.a)

for i = 1, 10 do
    _ENV["a" .. i] = true
end

print(a10)

For more info, read Chapter 14 of Programming in Lua.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luli