Hi, i really need to read a table from a table i already know how to read from normal tables but i dont know how to read a table thats inside a table heres my code:
local Table = {
question1={
41,
"Yes",
false
}
}
print(Table[1])
Hi, i really need to read a table from a table i already know how to read from normal tables but i dont know how to read a table thats inside a table heres my code:
local Table = {
question1={
41,
"Yes",
false
}
}
print(Table[1])
You are indexing a key “1” which is a number which doesn’t exist on the table.
Try:
local v = Table["question1"]
print(Table["question1"][1]) -- or 2, or 3...
omg thank you guys so much idk who to give the solution to but thank you guys so much
Extra info: You can do Table.question1
but if your key has spaces or isn’t a string (can be anything).
then you can do what i just did above.
I do have one more question how would i get a random table like if i have question 1 and question 2 and 3 then i could use math.random or smth to get a random table thats in a table?
You can, by using math.random
.
local v = Table.question1
local x = v[math.random(1, #v)]
--#v basically means how many items are in the question1 array, array means where a table only has numeric keys.
--From there we let the math.random function to choose randomly between indexes 1 to 3 and return the value itself
could it also work somth like:
local Table = {
Question1={
1,
"rwaer",
50
}
Question2={
41,
"cool",
"nice"
}
}
local question = math.random[1, #Table]
print(question[2])
If you’re attempting to accomplish a lookup-type thing, you could use a generic for loop to check.
local QuestionsTable = {
question1={
41,
"Yes",
false
},
question2={
52,
"Yes",
false
}
}
local function GetQuestion(num)
num = tostring(num)
for key, question in pairs(QuestionsTable) do
local temp = key:match("(%d+)$")
if temp ~= nil and num == temp then
return question
end
end
return nil
end
local q2 = GetQuestion(2)
print(type(q2)) -- table
print(table.unpack(q2)) -- 52 Yes false
As for getting a random question from the table, you could re-use the following code, but do…
...
local NumberOfQuestions = 0
for _ in next, QuestionsTable do
NumberOfQuestions = NumberOfQuestions + 1
end
...
local RandomQuestion = math.random(1, NumberOfQuestions)
local question = GetQuestion(RandomQuestion)
print(question)
Because this is similar to a dictionary, you can do something like this:
local Dict = {
["Dictionary"] = { -- OR without [ ] and " " so Just dictionary
Value1 = 1,
Value2 = true
}
}
You then can access it like:
local value = Dict.Dictionary.Value1 -- OR Dict["Dictionary"]["Value1"]
If you are unsure about giving the solution, ust give it to @TheLegendaryDragon99 because he is right and the first, so its only fair. We dont want this post to go on foverver! so better give the solution asap because the post will never close
is there any easier way to read it?
This is beautiful
I love tables and seeing all of these amazing people in here discussing about tables makes me super happy
Yes there is, look into Dictionaries, This is the thing you’re looking for.
Yeah, there are some modifications that could be done to make it more understandable. What I’d written allowed for a tad more flexibility. If your table’s keys started with Question
and ended with a number, you could do…
local QuestionsTable = {
Question1={
1,
"rwaer",
50
},
Question2={
41,
"cool",
"nice"
}
}
local question = QuestionsTable["Question" .. math.random(1, 2)]
The only issue with this is that:
math.random
, andQuestion
was misspelled or didn’t have consistent capitalization, it wouldn’t throw an error.The solution I gave prior would be the best way to go about it. Here’s something slightly more modified to make it more readable.
local QuestionsTable = {
Question1={
1,
"rwaer",
50
},
Question2={
41,
"cool",
"nice"
}
}
local TotalQuestions = 0 -- Since dictionaries do not have numbered indices, we have to keep track of the number of questions manually.
for _ in next, QuestionsTable do
TotalQuestions = TotalQuestions + 1 -- Increments the TotalQuestions 1 by, later used for math.random.
end
local function GetQuestion(num)
local QuestionInfo = nil -- No question found yet
for Question, Data in pairs(QuestionsTable) do
local temp = string.lower(string.sub(Question, 1, 8)) -- string.lower lowercases all letters, removing case-sensitivity. string.sub returns the letters between positions A and B.
if temp == "question" then
local qnum = tonumber(string.sub(Question, 9)) -- tonumber converts a string to a number (if applicable). string.sub returns every letter after the C position in the string.
if qnum ~= nil and qnum == num then -- Does Question<X> match what we want?
QuestionInfo = Data -- Set QuestionInfo to the question's data
break
end
end
end
return QuestionInfo -- Return the data
end
local RandomQuestionNum = math.random(1, TotalQuestions) -- Get a random number between 1 and the total number of questions in the table.
local QuestionToPresent = GetQuestion(RandomQuestionNum) -- Attempt to get the question with desired number.
if QuestionToPresent ~= nil then -- Did it manage to find the question?
print(QuestionToPresent)
-- code
end
Lmk if you have any further questions