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

Is there a way to check if a value is inside a table?

Asked by 10 years ago

In a simpler form: I am creating a script that contains true/false questions I have a table that contains questions, and two other tables. Those two other tables are named trueq1 and falseq1; and they contain values of questions that correspond to the table name. I need someone that can either link me to a wiki page or explain to me if there is a function that can check for values inside of tables. (i.e check if this value is under the trueq1 table or the falseq1 table)

In script form:

rqs1 = {"q1","q2","q3"}

trueq1 = {rqs1[1], rqs1[3]}
falseq1 = {rqs1[2]}

function isThisInTrue()
--check if this value is listed in the trueq1 table
end

1 answer

Log in to vote
0
Answered by 10 years ago
    tab = {"hello", {"1"}}
    tablecheck = function(newTable, item)
        foundValue = false
        for _, val in next, newTable do
        if val == item then 
            foundValue = true
            print("success")
            break
        elseif type(val) == "table" then
            tablecheck(val, item)
        end
    end
    return foundValue
end

val = tablecheck(tab, "1")

Output: success

.. Just call tablecheck twice for the different tables (and with different variables). And then move from there.

Ad

Answer this question