Messing with tables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Periwasmarkedasspam
Prole
Posts: 9
Joined: Sun Feb 08, 2015 5:30 am

Messing with tables

Post by Periwasmarkedasspam »

Hello! Today im creating some "Trivial" game, and im having problems with a table. Im getting this error
Error: Syntax error: questions.lua:42: unexpected symbol near '='

stack traceback:
[C]: ?
[C]: in function 'require'
main.lua:2: in main chunk
[C]: in function 'require'
[string "boot.lua"]:331: in function <[string "boot.lua"]:227>
[C]: in function 'xpcall'
Yes a strange "unexpected symbol" but i dont think is my fault, cause it still work on Windows (Love 0.9.0), but when i get in my Linux pc (Love 0.8.0) this keep failing. This is my code:

Code: Select all

--Table Defs.

questions = {}
	questions[1] = {} --Tier1
		questions[1][1] = {} --Q1
			questions[1][1][1] = "¿Qué profesion tenia Torre Isunza?" --Question1
			questions[1][1][2] = "Escultor" --Ans1
			questions[1][1][3] = "Escritor" --Ans2
			questions[1][1][4] = "Pintor" --Ans3
			questions[1][1][5] = 1 --RightAnswer
		questions[1][2] = {} --Q2
			questions[1][2][1] = "¿En qué año nació Torre Isunza?" --Question1
			questions[1][2][2] = "1734" --Ans1
			questions[1][2][3] = "1892" --Ans2
			questions[1][2][4] = "1987" --Ans3
			questions[1][2][5] = 2 --RightAnswer
		questions[1][3] = {} --Q3
			questions[1][3][1] = "¿En qué año murió Torre Isunza?" --Question1
			questions[1][3][2] = "2011" --Ans1
			questions[1][3][3] = "1982" --Ans2
			questions[1][3][4] = "1945" --Ans3
			questions[1][3][5] = 2 --RightAnswer
	questions[2] = {} --Tier2
		questions[2][1] = {} --Q1
			questions[2][1][1] = "QuestionTest2" --Question1
			questions[2][1][2] = "arrayAns12" --Ans1
			questions[2][1][3] = "arrayAns22" --Ans2
			questions[2][1][4] = "arrayAns32" --Ans3
			questions[2][1][5] = 2 --RightAnswer
		questions[2][2] = {} --Q2
			questions[2][2][1] = "QuestionTest2" --Question1
			questions[2][2][2] = "arrayAns12" --Ans1
			questions[2][2][3] = "arrayAns22" --Ans2
			questions[2][2][4] = "arrayAns32" --Ans3
			questions[2][2][5] = 2 --RightAnswer
		questions[2][3] = {} --Q3
			questions[2][3][1] = "¿En qué años ganó la primera y segunda medalla Torres Isunza?" --Question1
			questions[2][3][2] = "1989 y 1999" --Ans1
			questions[2][3][3] = "1890 y 1930" --Ans2
			questions[2][3][4] = "1922 y 1930" --Ans3
			questions[2][3][5] =  --RightAnswer
	questions[3] = {} --Tier3
		questions[3][1] = {} --Q3
			questions[3][1][1] = "QuestionTest3" --Question1
			questions[3][1][2] = "arrayAns13" --Ans1
			questions[3][1][3] = "arrayAns23" --Ans2
			questions[3][1][4] = "arrayAns33" --Ans3
			questions[3][1][5] = 3 --RightAnswer
		questions[3][2] = {} --Q3
			questions[3][2][1] = "QuestionTest3" --Question1
			questions[3][2][2] = "arrayAns13" --Ans1
			questions[3][2][3] = "arrayAns23" --Ans2
			questions[3][2][4] = "arrayAns33" --Ans3
			questions[3][2][5] = 3 --RightAnswer
		questions[3][3] = {} --Q3
			questions[3][3][1] = "QuestionTest3" --Question1
			questions[3][3][2] = "arrayAns13" --Ans1
			questions[3][3][3] = "arrayAns23" --Ans2
			questions[3][3][4] = "arrayAns33" --Ans3
			questions[3][3][5] = 3 --RightAnswer
clues = {}

--Define some Question values

questionNumber = 1
actualQuestion = "Test"
actualAns1 = "true"
actualAns2 = "false1"
actualAns3 = "false2"
actualRealAns = 1

questionTier = 1

function retrieveQuestion(questionTier)
	questionNumber = math.random(1)
	actualQuestion = questions[questionTier][questionNumber][1]
	actualAns1 = questions[questionTier][questionNumber][2]
	actualAns2 = questions[questionTier][questionNumber][3]
	actualAns3 = questions[questionTier][questionNumber][4]
	actualRealAns = questions[questionTier][questionNumber][5]
end
Any clue :D
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Messing with tables

Post by Nixola »

Code: Select all

         questions[2][3][5] =  --RightAnswer
   questions[3] = {} --Tier3
The above are the lines 41 and 42 of your code. The problem here is that questions[2][3][5] = is missing the righthand value. Lua thinks you wrote this:

Code: Select all

questions[2][3][5] = questions[3]
= {}
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Messing with tables

Post by s-ol »

Periwasmarkedasspam wrote:Hello! Today im creating some "Trivial" game, and im having problems with a table. Im getting this error

Code: Select all

--Table Defs.

questions = {}
	questions[1] = {} --Tier1
		questions[1][1] = {} --Q1
			questions[1][1][1] = "¿Qué profesion tenia Torre Isunza?" --Question1
			questions[1][1][2] = "Escultor" --Ans1
			questions[1][1][3] = "Escritor" --Ans2
			questions[1][1][4] = "Pintor" --Ans3
			questions[1][1][5] = 1 --RightAnswer
		questions[1][2] = {} --Q2
			questions[1][2][1] = "¿En qué año nació Torre Isunza?" --Question1
			questions[1][2][2] = "1734" --Ans1
			questions[1][2][3] = "1892" --Ans2
			questions[1][2][4] = "1987" --Ans3
			questions[1][2][5] = 2 --RightAnswer
		questions[1][3] = {} --Q3
			questions[1][3][1] = "¿En qué año murió Torre Isunza?" --Question1
			questions[1][3][2] = "2011" --Ans1
			questions[1][3][3] = "1982" --Ans2
			questions[1][3][4] = "1945" --Ans3
			questions[1][3][5] = 2 --RightAnswer
	questions[2] = {} --Tier2
		questions[2][1] = {} --Q1
			questions[2][1][1] = "QuestionTest2" --Question1
			questions[2][1][2] = "arrayAns12" --Ans1
			questions[2][1][3] = "arrayAns22" --Ans2
			questions[2][1][4] = "arrayAns32" --Ans3
			questions[2][1][5] = 2 --RightAnswer
		questions[2][2] = {} --Q2
			questions[2][2][1] = "QuestionTest2" --Question1
			questions[2][2][2] = "arrayAns12" --Ans1
			questions[2][2][3] = "arrayAns22" --Ans2
			questions[2][2][4] = "arrayAns32" --Ans3
			questions[2][2][5] = 2 --RightAnswer
		questions[2][3] = {} --Q3
			questions[2][3][1] = "¿En qué años ganó la primera y segunda medalla Torres Isunza?" --Question1
			questions[2][3][2] = "1989 y 1999" --Ans1
			questions[2][3][3] = "1890 y 1930" --Ans2
			questions[2][3][4] = "1922 y 1930" --Ans3
			questions[2][3][5] =  --RightAnswer
	questions[3] = {} --Tier3
		questions[3][1] = {} --Q3
			questions[3][1][1] = "QuestionTest3" --Question1
			questions[3][1][2] = "arrayAns13" --Ans1
			questions[3][1][3] = "arrayAns23" --Ans2
			questions[3][1][4] = "arrayAns33" --Ans3
			questions[3][1][5] = 3 --RightAnswer
		questions[3][2] = {} --Q3
			questions[3][2][1] = "QuestionTest3" --Question1
			questions[3][2][2] = "arrayAns13" --Ans1
			questions[3][2][3] = "arrayAns23" --Ans2
			questions[3][2][4] = "arrayAns33" --Ans3
			questions[3][2][5] = 3 --RightAnswer
		questions[3][3] = {} --Q3
			questions[3][3][1] = "QuestionTest3" --Question1
			questions[3][3][2] = "arrayAns13" --Ans1
			questions[3][3][3] = "arrayAns23" --Ans2
			questions[3][3][4] = "arrayAns33" --Ans3
			questions[3][3][5] = 3 --RightAnswer
clues = {}
Your question format is also terribly redundant, you only need to write one third as much for the exact same results. Read this about nested table literals: http://www.lua.org/pil/3.6.html

Code: Select all

questions = {
    {
       {"¿Qué profesion tenia Torre Isunza?", "Escultor", "Escritor", "Pintor", 1},
       {"¿En qué año nació Torre Isunza?", "1734", "1892", "1987", 2},
       {"¿En qué año murió Torre Isunza?", "2011", "1982", "1945", 2}
     },
    {
       {"QuestionTest2", "arrayAns12", "arrayAns22", "arrayAns32", 2},
       {"QuestionTest2", "arrayAns12", "arrayAns22", "arrayAns32", 2},
       {"¿En qué años ganó la primera y segunda medalla Torres Isunza?", "1989 y 1999", "1890 y 1930", "1922 y 1930" <YOUR ERROR WAS HERE>}
    },
    {
       {"QuestionTest3", "arrayAns13", "arrayAns23", "arrayAns33", 3 },
       {"QuestionTest3", "arrayAns13", "arrayAns23", "arrayAns33", 3 },
       {"QuestionTest3", "arrayAns13", "arrayAns23", "arrayAns33", 3 }
    }
}
See how much easier to read and edit that is?

I would also advise to use a format that is a little more verbose you your don't confuse the indices (was the correct answer question[4] or question[5]???):

Code: Select all

questions = {
  {
    { question = "What is the most recent stable release of the LÖVE framework?", correct = 2, answers = { "0.8.1", "0.9.2", "10.0.0" } },
    { question = "What is the most recent stable release of the LÖVE framework?", correct = 2, answers = { "0.8.1", "0.9.2", "10.0.0" } },
    { question = "What is the most recent stable release of the LÖVE framework?", correct = 2, answers = { "0.8.1", "0.9.2", "10.0.0" } }
  },
  {
    { question = "asd", correct = 4, answers = { "bla", "bleh", "blergh", "rem" } },
    ......
  }
}
This is not only much easier to read and use, it also easily allows variable amount of answers (the "asd" question has 4 answers, the others have three, and you can tell just by #q.answers).

Code: Select all

local tier = 1
for _, q in ipairs(questions[tier]) do
  print(q.question)
  for _,a in ipairs(q.answers) do
    print( a )
  end
  local answer = q.read()
  print( "correct answer: " .. q.answers[q.correct] )
  if tonumber(answer) == q.correct then
    print( "you got it right!" )
  end
end
look how self-explanatory that example is!

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Periwasmarkedasspam
Prole
Posts: 9
Joined: Sun Feb 08, 2015 5:30 am

Re: Messing with tables

Post by Periwasmarkedasspam »

Thanks!! :D You saved my day!
This is a good community, not like other lua programmer places...
Thanks again! :awesome: :awesome: :awesome:
User avatar
I~=Spam
Party member
Posts: 206
Joined: Fri Dec 14, 2012 11:59 pm

Re: Messing with tables

Post by I~=Spam »

Yes you should write tables the way S0lll0s said. There are a lot a reasons. The code is easier to read. But also the table is initialized faster because lua can declare the table all at once.
My Tox ID: 0F1FB9170B94694A90FBCF6C4DDBDB9F58A9E4CDD0B4267E50BF9CDD62A0F947E376C5482610
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Messing with tables

Post by s-ol »

I~=Spam wrote:Yes you should write tables the way S0lll0s said. There are a lot a reasons. The code is easier to read. But also the table is initialized faster because lua can declare the table all at once.
Also errors like The one you experienced are much easier to find and when you modify a question or move it to another spot it's simply copy&paste instead of changing indices all over the place.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest