Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

calling an array within a table?

Asked by 6 years ago

not sure if this is possible, BUT! i made an account just to ask this

so i have this table, a "dictionary" (though the names arent really relevant) that refer to arrays.

local table = {
    ["Thing"] = {1,0},
    ["OtherThing"] = {2,0},

}

what i don't know is - how would you go about referencing the specific values within the arrays? i had an idea that i could be sneaky and do something like

local idtable = table[1]
    local arraynumfromtab = idtable[1]
    local otherarraynumfromtab = idtable[2]

but the above obviously doesn't work. so can i get a pointer here?

1 answer

Log in to vote
0
Answered by
chomboghai 2044 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

I'm going to start of by saying that isn't "calling" a list; you call a function, you create a list.

Anyway, of course it's possible! You can store data inside a dictionary, and that data can take almost any form. You could even create a dictionary inside a dictionary.

The way you referenced it would definitely work! Except you are using a dictionary, so the line local idtable = table[1] doesn't work since there is no key 1. The keys are Thing and OtherThing.

If you think about it, you set idtable to be defined as the first item in the dictionary (let's imagine you fixed the [1] error). That means saying arraynumfromtab = idtable[1] is perfectly valid. It's equivalent to saying this:

local idtable = {1, 0}
local arraynumfromtab = idtable[1] -- 1
local otherarraynumfromtab = idtable[2] -- 0

And this definitely works!

You can also use a shortcut of indexing those elements:

local table = {
    ["Thing"] = {1,0},
    ["OtherThing"] = {2,0},
}

local arraynumfromtab = table["Thing"][1] -- 1
local otherarraynumfromtab = table["Thing"][2] -- 0

Obviously the way mentioned previously would be better for this case, but this shortcut is good to know.

I hope this helps! :)

0
thanks gamer NinjaboySC 11 — 6y
0
If @chomboghai answered your question, please make sure to accept his or her answer. RAYAN1565 691 — 6y
Ad

Answer this question