Generating a table with lots of keys

I have an example table like this:

["1"]  = {100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

I want to make a script that generates this kind of table, i know how to use table.insert() though.

Here’s my question.
I want to make a function that generates all those 0 and 100’s at once, and not doing it all manually.

…how do I do this?

I know you may be really confused by this but I don’t have a better way explaining all this.

If I understand correctly, do you wish to get a table that has only values 0 and 100? If so, randomized? I can think of a quick solution but want to ensure I’m understanding this correctly.

Not randomized, but you can set it to stuff like this:

Just an example of tables I have since I’m making a system where you can put as many children in a parent as you want and it will automatically add the table with keys [“Name”] = numbersHere.

So it’s not hardcoded to be 1 to 24 only.

So you want something like this?

function generateTableWithLotsOfKeysOrSomethingLikeThat(amountOfKeys, positionOf100)
    local row = table.create(amountOfKeys, 0)
    row[positionOf100] = 100
    return row
end

Then repeat this for the height?

Correct, I want it to repeat it for the height like shown as an example in my 2 giant tables of keys.

function generateFullTable(positionsOf100s, depth)
    local grid = table.create(#positionsOf100s)

    for i,pos in positionsOf100s do
        grid[i] = generateTableWithLotsOfKeysOrSomethingLikeThat(depth, pos)
    end

    return grid
end

Do note that this makes the keys be numbers instead of strings like in your example.

Could you send an example of how I can fire both of the functions generateTableWithLotsOfKeysOrSomethingLikeThat and generateFullTable?

I’m aware i need to input numbers inside the () when firing it but since there are 2 functions I am quite confused how to run it.

generateTableWithLotsOfKeysOrSomethingLikeThat (you should use a different name) is for generating a single row.
generateFullTable for a full table.

(you can also combine them if you never need to generate a single row)

the row generator takes in 2 numbers
and the full generator takes in an array of numbers and a single number

Here’s what comes up in the output.

Is there something else I need to add to the function?
image

Line 20 is where it errors, which is

local grid = table.create(#positionsOf100s)

positionsOf100s has to be an array of positions

My bad, I didn’t realize there was {} needed to be added, I was quite blind.

If I were to generate this table:
image

What parameters do I need to put for generateFullTable({numbersHere}, numberHere)?

generateFullTable({24, 23, 22, 21, 20, etc… , 5, 4, 3, 2, 1}, 24)

That works, but Is there a way I can generate it to have it look like this?
image

At the moment the code only adds
image

No table of 0 and 100s…

I’m sorry I might be quite stupid.

Could you show the code you are using again? in full?

Here’s the code you gave me, since I haven’t found a way for myself.

Here’s what I need to have generated:
image

The strings that are now numbers (it shouldn’t make a difference for what I need it for), should consist a table of 100s and 0s, kind of like shown in the screenshot.

This is the result of line 7, not line 17

My bad, here’s result of 17…
image
It goes on…
image

But makes another row of 1, 2, 3, 4, 5… etc.

Which basically means tables in tables… if I’m correct.

image
This is what it looks like when you print {0,0,100,0,0}.

I’m aware of that but here’s what it currently does…

It adds 1, 2, 3, 4 … again, if you compare it to the screenshot I sent of what it should do…

image

example = { is like [“left”], which should be unaffected.

No it doesn’t


This is what it’s showing when you print it

example = {
    [1] = {
        [1] = 100,
        [2] = 0,
        [3] = 0,
        [4] = 0,
    },
    [2] = {
        [1] = 0,
        [2] = 100,
        [3] = 0,
        [4] = 0,
    },
    [3] = {
        [1] = 0,
        [2] = 0,
        [3] = 100,
        [4] = 0,
    },
    [4] = {
        [1] = 0,
        [2] = 0,
        [3] = 0,
        [4] = 100,
    }
}

Which is the same as

example = {
    [1] = {100,0,0,0},
    [2] = {0,100,0,0},
    [3] = {0,0,100,0},
    [4] = {0,0,0,100}
}

Which is btw the same as

example = {
    {100,0,0,0},
    {0,100,0,0},
    {0,0,100,0},
    {0,0,0,100}
}