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

From a table randomly select any key with a specific value range?

Asked by 5 years ago
Edited 5 years ago

Using the table posted below I would like to find every dictionary key with a specific value ranging from 0 to 1 (A to D) and ignore the rest (E to H). Can anyone give me advice as to how I can approach this?

ExampleTable = {
    ["A"] = 0,
    ["B"] = 0,
    ["C"] = 1,
    ["D"] = 1,
    ["E"] = 2,
    ["F"] = 2,
    ["G"] = 3,
    ["H"] = 3,
}

Answered by yHasteeD

1 answer

Log in to vote
-1
Answered by 5 years ago
Edited 5 years ago

With the example provided, something like this should work:

for i,v in pairs(ExampleTable) do
    if i == "A" or i == "B" or i == "C" or i == "D" then -- Checking if the key is A to D
        print(v) -- Printing the value
    end
end
0
or you could just math.random it Arkrei 389 — 5y
0
@Arkrei you can't use math.random() on a table with string keys (dictionary), it only works on arrays Psudar 882 — 5y
Ad

Answer this question